rounding doubles to set # decimal places
-
Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.
-
Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.
I don't do a lot of numeric stuff anymore so take this with a pinch of salt. First off I don't know any C++ standard library facilities that would help. C had
floor
andceil
which take doubles and return an integer either truncating the value or rounding up to the next highest number. With these andpow
you can write something that'll be good enough for most things. Off the top of my head it'd look like:double round_to( double number, unsigned number_of_decimal_places )
{
double exponent( pow( 10, number_of_decimal_places ) );
number *= exponent; // disaster line!
number += 0.5;
return floor( number ) / exponent;
}which is a bit cheesy - it won't work for negative numbers or any number that causes the line with the "disaster line!" comment to overflow an integer. However they might give you a handle if no one comes up with a better idea! Cheers, Ash Edited to remove a spurious brace...
modified on Friday, June 18, 2010 3:52 PM
-
Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.
You may use the dirty old town's trick:
double rounded(double x, int n)
{
double d = pow(10, (double)n);
return ((int)(x * d + .5))/d;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may use the dirty old town's trick:
double rounded(double x, int n)
{
double d = pow(10, (double)n);
return ((int)(x * d + .5))/d;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I don't do a lot of numeric stuff anymore so take this with a pinch of salt. First off I don't know any C++ standard library facilities that would help. C had
floor
andceil
which take doubles and return an integer either truncating the value or rounding up to the next highest number. With these andpow
you can write something that'll be good enough for most things. Off the top of my head it'd look like:double round_to( double number, unsigned number_of_decimal_places )
{
double exponent( pow( 10, number_of_decimal_places ) );
number *= exponent; // disaster line!
number += 0.5;
return floor( number ) / exponent;
}which is a bit cheesy - it won't work for negative numbers or any number that causes the line with the "disaster line!" comment to overflow an integer. However they might give you a handle if no one comes up with a better idea! Cheers, Ash Edited to remove a spurious brace...
modified on Friday, June 18, 2010 3:52 PM
-
Excellent, this did exactly what I was needing. Thanks! One thing I noticed was when I used -265.3215894 with n=2, it returns -265.31 instead of -265.32 Any thoughts on this?
As Aescleal stated, that doesn't work for negative numbers. However you may test for the sign and act accordingly (subtracting
.5
, instead of adding it). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]