#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)
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 mini sites like this. We hope you find the site 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 on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.