Sign in to follow this  
Luchie505

Help with rounding numbers( C++ )

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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);

// OR

int rounded = (int)number;


Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by _fastcall

Round 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 this post


Link to post
Share on other sites
Quote:
Original post by wlw_wl
Quote:
Original post by _fastcall

Round 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this