We will get nearest integer part of a decimal value by using round()
function. If the decimal part is less than .5 then lower side integer is returned. If decimal part is more than or equal .5 then higher side is returned.
round(x)
x = input number
Example with integer variables
#include
#include
int main(void){
int x1,x2,x3,x4;
x1=round(13.345);
x2=round(12.656);
x3=round(-13.21);
x4=round(-14.67);
printf("x1=%d , x2=%d,x3=%d,x4=%d", x1,x2,x3,x4);
return 0;
}
The above code will give this output.
x1=13, x2=13, x3=-13,x4=-15
Example with float variables
#include <stdio.h>
#include <math.h>
int main(void){
float x1,x2,x3,x4;
x1=round(13.545);
x2=round(12.656);
x3=round(-13.51);
x4=round(-14.67);
printf("x1=%f , x2=%f,x3=%f,x4=%f", x1,x2,x3,x4);
return 0;
}
Output is here
x1=14.000000, x2=13.000000, x3=-14.000000,x4=-15.000000