#include <math.h>
rint() Round to nearest integer
(int) does not round values correctly!!!!!
Converting a value with decimal places to an int using (int) will convert ignoring the decimal places completly. If you want a value of say 5.6 to convert to and int of 6 and not 5 then use: (int)rint()
double rint (double x)
float rintf (float x)
long double rintl (long double x)
The rint() function rounds a double to the nearest integer. It’s output is still a double;
For instance if double d = 5.83
x = (WORD)d;
Would produce x=5
x = (WORD)rint(d);
Would produce x=6
Doing this in simple code without rint()
double my_double = 1.500;
my_double = round_d(my_double);
double round_d(double number)
{
return (double)((number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5));
}
floor() Round downwards to the nearest integer
double floor (double x)
float floorf (float x)
long double floorl (long double x)
ceil() Round upwards to the nearest integer
double ceil (double x)
float ceilf (float x)
long double ceill (long double x)
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.