round() in C

round() returns the nearest integral value as a floating-point result. Halfway cases are rounded away from zero.

Syntax Top ↑

double round(double x);

Example 1 Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value = 5.4;

    printf("round(5.4): %.1f\\n", round(value));
    return 0;
}

Example 2 Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value = 5.5;

    printf("round(5.5): %.1f\\n", round(value));
    return 0;
}

Example 3 Top ↑

#include <math.h>
#include <stdio.h>

int main(void) {
    double value = -5.5;

    printf("round(-5.5): %.1f\\n", round(value));
    return 0;
}

Preserved round() Variations Top ↑

Round User Input

#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;
}

Round an Array of Values

#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;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer