Public Group

# Help with rounding numbers( C++ )

This topic is 3873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Luchie505Hi, 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 places2) Add 0.53) 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.

##### Share on other sites
Quote:
 Original post by deadstarResult in both cases is 4.

In C++, casts from floating point to integer types round down.

##### Share on other sites
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

##### Share on other sites
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;

##### Share on other sites
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;

##### Share on other sites
In C++ ^ is the xor operator, not exponentiation. To do exponentiation, you can use the pow() function.

##### Share on other sites
ok thx im gonna try that rite now

##### Share on other sites
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.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013628
×