Help with rounding numbers( C++ )

Started by
15 comments, last by rolkA 15 years, 11 months ago
Hi, I am trying to put convert this algorithm for rounding numbers into a code, but I am having trouble with 1 of the steps. I would appreciate it if someone could help me or explain to me what is step 3. thank you. Here is the algorithm: 1) Multiply the number by 10 number of places 2) Add 0.5 3) Truncate the result (you can do this by casting to int) 4) Divide result by 10 number of places
Advertisement
It means to convert (or CAST) the variable to a different type (integer).

C way:

float OldNumber = 3.8;int NewNumber = (int)OldNumber;



C++ way:

float OldNumber = 3.8;int NewNumber = static_cast<int>OldNumber;


Result in both cases is 4.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Quote:Original post by Luchie505
Hi, I am trying to put convert this algorithm for rounding numbers into a code, but I am having trouble with 1 of the steps. I would appreciate it if someone could help me or explain to me what is step 3. thank you.

Here is the algorithm:
1) Multiply the number by 10 number of places
2) Add 0.5
3) Truncate the result (you can do this by casting to int)
4) Divide result by 10 number of places


Use cast, for example :

float convert(float in)
{
int temp = in *10 +0.5;
return ((float)temp / 10);
}

in* 10 +0.5 return a float, then the result is converted to a integer. then the integer is reconvert to a float and it is divided by 10.

Quote:Original post by deadstar
Result in both cases is 4.

In C++, casts from floating point to integer types round down.
Do a few sample problems:

Round from 1.2345678 to 1.235, the number of digits (n) is 3:

1) Multiply the number by 10 number of places
	x * 10 ^ n 	= 1.2345678 * 10 ^ 3	= 1.2345678 * 1000	= 1234.5678


2) Add 0.5 (Here is the key:)
	x + 0.5	= 1234.5678 + 0.5	= 1235.0678


3) Truncate the result (you can do this by casting to int)
	int( x )	= floor( 12345.0678 )	= 1235


4) Divide result by 10 number of places
	x / 10 ^ n 	= 1235 / 1000	= 1.235

This algorithm presumes you are working with real numbers (either float or double). Step 3 is referring to an intentional loss of precision resulting from the conversion of this real number to an integer. For example, 3.499 as an integer becomes 3, and 7.999999 as an integer becomes 7. So the algorithm has you add 0.5 to the number, thereby pushing numbers with decimal values >= 0.5 into the next integer range (as in 5.6 + 0.5 = 6.1 which is 6 as an integer, where 5.49 + 0.5 = 5.99 which is still 5).

Long story... well... long, you need to explicitly convert your real number to an int for step 3

float number;// do some stuff with number...int rounded = static_cast<int>(number);// ORint rounded = (int)number;
thanks a lot for all your help and fast response. I did the algorithm, but I do not no why my code still does not work properly. for example, when i type in 32.893 for my number and then decimal place 3, it still returns 32. it seems to always display the numbers before the decimal places no matter what the number is. here is my code.

double number;
int decimalPlaces;
cout << "Enter a positive number to be rounded ( enter -1 to quit):";
cin >> number;
cout << " Enter number of decimal places:";
cin >> decimalPlaces;

number *= 10 ^ decimalPlaces;
number += 0.5;
int rounded = static_cast<int>(number);
rounded /= 10 ^ decimalPlaces;
cout << rounded;
In C++ ^ is the xor operator, not exponentiation. To do exponentiation, you can use the pow() function.
ok thx im gonna try that rite now
I get this error 'call of overloaded `pow(int, int&)' is ambiguous' on this line.

number *= pow(10, decimalPlaces);

if i cannot use int for the pow function, then how will i no how much decimal places the user has entered? thank again for your time everyone.

This topic is closed to new replies.

Advertisement