Click here to Skip to main content
15,894,330 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
When the user inputs 2.09 it is multiplied by 100 the output comes out as 208.
Below is the program and the output:

C++
#include<iostream>

using namespace std;

int main ( )
{

	float userNumber;
	int change = 0;               
	int n = 100;

	cout << "Enter cash: ";

	cin >> userNumber;

	cout << "\nuserNumber value: " << userNumber << "\n"; 

	change = userNumber * n;

	cout << "\nChange after userNumber * 100: " << change  << " userNumber: " <<     userNumber  <<   "\n\n";                                                 
}

-------------------------Below is the output------------------


Enter cash: 2.09

userNumber value: 2.09

Change after userNumber * 100: 208 userNumber: 2.09


What I have tried:

I have gone to several sites

Add 0.5 before casting (if x > 0) or subtract 0.5 (if x < 0), because the compiler will always truncate.
Posted
Updated 23-Nov-16 21:21pm
v2
Comments
Philippe Mori 24-Nov-16 0:05am    
Then if you have found the solution by adding/substracting 0.5, then what is the question/problem. Once, you know the rule, you write code so that it works as expected.
Philippe Mori 24-Nov-16 0:06am    
Is it that hard to use a code block to format your code.
Patrice T 24-Nov-16 2:48am    
What is the question ?

Use round[^].
 
Share this answer
 
The reason for this behaviour is that most floating point numbers can't be represented exactly.

2.09 is such a number. When converting the string to a single precision (float) the stored value is 2.089999914. So the result of the multiplication is 208.9999914. Because casting to int is just truncating, the result is 208.

Even using double precision (double) would not help because that can't store the exact value 2.09 too.

The solution is to use round() as suggested by CPallini or adding 0.5:
C++
float floatResult = userNumber * n;
change = static_cast<int>(floatResult < 0 ? floatResult - 0.5f : floatResult + 0.5f);</int>

But note that the above should only be used when you know that the result will fit in an integer (does not generate an overflow).
 
Share this answer
 
if you use setprecision, it shows what has happened. The decimal has been truncated. Use ceil() in math.h to round up.

C++
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main ( )
{

float userNumber;
float h,k;
int change = 0;
int n = 100;

cout << "Enter cash: ";

cin >> userNumber;

cout << "\nuserNumber value: " << userNumber << "\n";
h=userNumber*n;

cout << setprecision(10) << h;

k=ceil(userNumber*n);

change = k;

cout << "\nChange after userNumber * 100: " << change << " userNumber: " << userNumber << "\n\n";
}
 
Share this answer
 
Comments
Philippe Mori 24-Nov-16 0:03am    
If you use ceil, then you would have the opposite problem where the result is higher than expected for some number. Thus rounding should be used.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900