#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)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *