#include <stdio.h>
int main() {
printf("\nNumber = %.2f", round(12.38)); // Number = 12.00
printf("\nNumber = %.2f", round(12.89)); // Number = 13.00
printf("\nNumber = %.2f", round(-12.32)); // Number = -12.00
printf("\nNumber = %.2f", round(-12.95)); // Number = -13.00
return 0;
}
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;
}