|
- Dev C++ 5.11
- Dev C++ Rounding Video
- Dev C Rounding Worksheets
- C++ Rounding Floats
- Dev C++ Rounding Calculator
Dev C++ 5.11
Helios - can you explain a little more how you got a set precision effect from ceil? |
The round function in C returns the integral value that is nearest to the argument, with halfway cases rounded away from zero. Round to nearest Returns the integral value that is nearest to x, with halfway cases rounded away from zero. Header provides a type-generic macro version of this function. Jul 19, 2005 rounding a float/double to nearest 1/10th. C / C Forums on Bytes. Home topics c / c questions rounding a float/double to nearest 1/10th. Somehow I missed that round was a standard function. I'd be happy if you could point out where I can find it, so that I can get rid of my own.
ceil(num*pow(10,x))/pow(10,x) |
![Dev C++ Rounding Dev C++ Rounding](/uploads/1/2/6/0/126068168/266242609.png)
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
Dev C++ Rounding Video
Just a typo:
Dev C Rounding Worksheets
The first 'x' you have must be your number and the rest 'x's the number of decimals points you want.C++ Rounding Floats
[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:
Dev C++ Rounding Calculator
ceil( ( num * pow( 10,x ) ) - 0.49 ) / pow( 10,x );