# Help with rounding numbers( C++ )

This topic is 3575 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.

##### Share on other sites
pow() is actually three different functions pow(float, float), pow(double, double) and pow(long double, long double). When you pass two ints as arguments the compiler can't figure out which one you want to use. You can solve this problem by casting both arguments to float (or double or long double).

##### Share on other sites
hey thx i solved that problem but for some reason my code still doesnt work properly. No matter what number i enter for the decimal place, it still gives me the number no rounded without decimal. for example if i put in 32.567 and round to 3 decimal place, it still gives me 32. 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 *= pow(10.0, decimalPlaces);
number += 0.5;
int rounded = static_cast<int>(number);
rounded /= pow(10.0, decimalPlaces);
cout << rounded;

##### Share on other sites
rounded is an integer, so you can't have any decimal in it.

int rounded = static_cast<int>(number);double result = rounded / pow(10.0, decimalPlaces);cout << result ;`

##### Share on other sites
yes it finally works, thank you so much and everyone for your time.

##### Share on other sites
Casting to int is a very bad idea, because lots of floating point numbers have an integral part that is way too high for an int to hold. I recommend including <cmath> and use floor() instead.

##### Share on other sites
Quote:
 Original post by _fastcallRound from 1.2345678 to 1.235

that's ceiling.

1.2345678 rounded to 3 digits after comma is 1.234 (as fourth 5 means "floor it").

##### Share on other sites
Quote:
Original post by wlw_wl
Quote:
 Original post by _fastcallRound from 1.2345678 to 1.235

that's ceiling.

1.2345678 rounded to 3 digits after comma is 1.234 (as fourth 5 means "floor it").

No, mathematically 1.05 is rounded to 1.1, like 1.5 is rounded to 2. That's why int(x + 0.5), x being positive, works.

1.2345678 rounded to 3 digits after comma is 1.235.