round() returns the nearest integral value as a floating-point result. Halfway cases are rounded away from zero.
double round(double x);#include <math.h>
#include <stdio.h>
int main(void) {
double value = 5.4;
printf("round(5.4): %.1f\\n", round(value));
return 0;
}#include <math.h>
#include <stdio.h>
int main(void) {
double value = 5.5;
printf("round(5.5): %.1f\\n", round(value));
return 0;
}#include <math.h>
#include <stdio.h>
int main(void) {
double value = -5.5;
printf("round(-5.5): %.1f\\n", round(value));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double value;
printf("Enter a number: ");
if (scanf("%lf", &value) != 1) {
printf("Invalid input.\n");
return 1;
}
printf("Rounded value: %.0f\n", round(value));
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
double values[] = {3.14, -2.73, 5.99, -7.01};
size_t count = sizeof values / sizeof values[0];
for (size_t i = 0; i < count; i++) {
printf("round(%.2f) = %.0f\n",
values[i], round(values[i]));
}
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.