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 #:
//headers & such
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);
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:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
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.
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?
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 );
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