### Rounding Numbers in c++

For example
I have happening apportionment numbers 12.456789
How can we make a product that appears after the interval only two of any 12.45
Is it possible to bring the product to the previous 12.46
Can also be rounded to 13
I believe this might work to round to nearest whole #:
float num;
cin >> num;
if( (num + 0.5) >= (int(num) + 1) )
cout << num << " rounds to " << int(num)+1;
else
cout << num << " rounds to " << int(num);
You can use the "setprecision()" and "fixed" flags of the stream.
http://www.cplusplus.com/reference/iostream/manipulators/setprecision.html
http://www.cplusplus.com/reference/iostream/manipulators/fixed.html

The setprecision() sets how many decimal points you will have and the fixed is just because when you use double it converts it to scientific mode.
Here is a small example:

 ``12345678910`` ``````#include #include using namespace std; int main(){ double num = 12.456789; cout << num << '\n'; //Outputs 12.4568 cout << fixed << num << '\n'; //Outputs 12.456789 cout << setprecision(3) << fixed << num << '\n'; //Outputs 12.457 return 0; }``````

Hope this helps
What I've always done to leave only y decimal places in x is floor(x*pow(10,x))/pow(10,x). For your case, this can be simplified as floor(x*100)/100. You can also change floor to ceil to round up (for example, from 12.4510001 to 12.46).
Finally to round up 12.456789 to 13, use ceil(x), as well.

These functions are defined in cmath or math.h.
Last edited on
ceil() always rouds it up and floor always rounds it down.
If you need accuracy, you are loosing something from both, you can't use them...
 ceil() always rouds it up and floor always rounds it down.
you can use floor(x+0.5) or ceil(x-0.5)
If you are going to use ceil
here's an example:

 `` `` ``fArea = ceil (fLength * fWidth)``

That will round to the next highest whole number. Floor uses the same syntax and will round to the next lowest whole number. If you use setprecision() you are going to be hacking a number off at a certain number... not rounding. Just do not forget to include your math header (cmath for most, math.h for older development environments).

Helios - can you explain a little more how you got a set precision effect from ceil?
"Set precision effect"? You mean this?

ceil(x*pow(10,x))/pow(10,x)

I've noticed a tendency among us to ignore the difference between rounding and truncation.
By "setprecision" effect, I am saying the specificity of rounding to a certain decimal place. I see the code, but I am not understanding how/why it has the effect it does (I am a very new programmer).
 Helios - can you explain a little more how you got a set precision effect from ceil?
 ceil(num*pow(10,x))/pow(10,x)

What he is doing is multiply the number with a power of ten (the power depends from the number of decimal point you want), use the ceil() function to eliminate the remaining decimal points and then divide with the same power of ten again to put the decimal points (the one that we need the precision to be) back to their original place.

For example if you have the number 12.456789 and you want a precision of 2 decimal numbers then you will have:

12.456789 * 100 (10 to the 2nd power) = 1245.6789
ceil( 1245.6789 ) = 1246
1246 / 100 (10 to the 2nd power) = 12.46

So you have the presicion you want.

@helios
Just a typo:
The first 'x' you have must be your number and the rest 'x's the number of decimals points you want.

[EDIT] This formula doesn't round the number correctly, just eliminates the decimal points after the precision you want and rounds up the number.

This would round it correctly:
`ceil( ( num * pow( 10,x ) ) - 0.49 ) / pow( 10,x );`
Last edited on
Mitsakos: Haha. You're right. That might be why he was getting confused. What I actually meant to write was floor(x*pow(10,y))/pow(10,y).
No, it doesn't round the number. It's not supposed to, either. It truncates it up.
By the way, I think your formula there should be +.5, not +.49.
I tryed -0.5 but if the number is 12.455 it will not round it correctly, because ceil() rounds up if it is greater than .0
So a number 12.455 will not round it up, because it will be 12.45 and this is the only way ceil() doesn't round up :D
Last edited on
std::cout <<ceil(12.455-.5)<<std::endl;
std::cout <<floor(12.455+.5)<<std::endl;
These two work fine for me.

12.455-.5==11.955, and ceil(11.955)==12.
However, 12.495-.49==12.005, and ceil(12.005)==13.
I just posted an article (with code) on this if anyone cares. (This thread prompted the article.)

Enjoy! :-)
std::cout << ceil( 12.5 - 0.5 ) << std::endl;