You could re-invent the wheel, as many other answers suggest. Alternately, you could use someone else's wheel -- I'd suggest Newlib's, which is BSD-licensed and intended for use on embedded systems. It properly handles negative numbers, NaNs, infinities, and cases which are not representable as integers (due to being too large), as well as doing so in an efficient manner that uses exponents and masking rather than generally-costlier floating-point operations. In addition, it's regularly tested, so you know it doesn't have glaring corner-case bugs in it.

The Newlib source can be a bit awkward to navigate, so here are the bits you want:

Float version: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/sf_round.c;hb=master

Double version: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/s_round.c;hb=master

Word-extraction macros defined here: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/fdlibm.h;hb=master

If you need other files from there, the parent directory is this one: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=tree;f=newlib/libm/common;hb=master

For the record, here's the code for the float version. As you can see, there's a bit of complexity required to deal with all the possible cases correctly.

float roundf(x)
{
  int signbit;
  __uint32_t w;
  /* Most significant word, least significant word. */
  int exponent_less_127;

  GET_FLOAT_WORD(w, x);

  /* Extract sign bit. */
  signbit = w & 0x80000000;

  /* Extract exponent field. */
  exponent_less_127 = (int)((w & 0x7f800000) >> 23) - 127;

  if (exponent_less_127 < 23)
    {
      if (exponent_less_127 < 0)
        {
          w &= 0x80000000;
          if (exponent_less_127 == -1)
            /* Result is +1.0 or -1.0. */
            w |= ((__uint32_t)127 << 23);
        }
      else
        {
          unsigned int exponent_mask = 0x007fffff >> exponent_less_127;
          if ((w & exponent_mask) == 0)
            /* x has an integral value. */
            return x;

          w += 0x00400000 >> exponent_less_127;
          w &= ~exponent_mask;
        }
    }
  else
    {
      if (exponent_less_127 == 128)
        /* x is NaN or infinite. */
        return x + x;
      else
        return x;
    }
  SET_FLOAT_WORD(x, w);
  return x;
}
Answer from Brooks Moses on Stack Overflow
🌐
W3Schools
w3schools.com › c › ref_math_round.php
C Math round() Function
C Examples C Real-Life Examples ... printf("%f", round(-5.9)); Try it Yourself » · The round() function rounds a number to the nearest integer....
🌐
Scaler
scaler.com › home › topics › c round() function
C round() Function - Scaler Topics
March 27, 2024 - The C round() function is one of ... The round() function in C returns the nearest integer value (rounded value) of the given float, integer, or double number based on the decimal part of the number....
🌐
GeeksforGeeks
geeksforgeeks.org › c language › round-function-in-c
round() Function in C - GeeksforGeeks
July 5, 2024 - C round() is a built-in library function that rounds a floating-point number to the nearest integer. If the fractional part of the number is 0.5 or greater, the argument is rounded away from zero.
🌐
Cppreference
cppreference.com › w › c › numeric › math › round.html
round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl - cppreference.com
1-3) Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
Top answer
1 of 10
23

You could re-invent the wheel, as many other answers suggest. Alternately, you could use someone else's wheel -- I'd suggest Newlib's, which is BSD-licensed and intended for use on embedded systems. It properly handles negative numbers, NaNs, infinities, and cases which are not representable as integers (due to being too large), as well as doing so in an efficient manner that uses exponents and masking rather than generally-costlier floating-point operations. In addition, it's regularly tested, so you know it doesn't have glaring corner-case bugs in it.

The Newlib source can be a bit awkward to navigate, so here are the bits you want:

Float version: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/sf_round.c;hb=master

Double version: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/s_round.c;hb=master

Word-extraction macros defined here: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libm/common/fdlibm.h;hb=master

If you need other files from there, the parent directory is this one: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=tree;f=newlib/libm/common;hb=master

For the record, here's the code for the float version. As you can see, there's a bit of complexity required to deal with all the possible cases correctly.

float roundf(x)
{
  int signbit;
  __uint32_t w;
  /* Most significant word, least significant word. */
  int exponent_less_127;

  GET_FLOAT_WORD(w, x);

  /* Extract sign bit. */
  signbit = w & 0x80000000;

  /* Extract exponent field. */
  exponent_less_127 = (int)((w & 0x7f800000) >> 23) - 127;

  if (exponent_less_127 < 23)
    {
      if (exponent_less_127 < 0)
        {
          w &= 0x80000000;
          if (exponent_less_127 == -1)
            /* Result is +1.0 or -1.0. */
            w |= ((__uint32_t)127 << 23);
        }
      else
        {
          unsigned int exponent_mask = 0x007fffff >> exponent_less_127;
          if ((w & exponent_mask) == 0)
            /* x has an integral value. */
            return x;

          w += 0x00400000 >> exponent_less_127;
          w &= ~exponent_mask;
        }
    }
  else
    {
      if (exponent_less_127 == 128)
        /* x is NaN or infinite. */
        return x + x;
      else
        return x;
    }
  SET_FLOAT_WORD(x, w);
  return x;
}
2 of 10
22
int round(double x)
{
    if (x < 0.0)
        return (int)(x - 0.5);
    else
        return (int)(x + 0.5);
}
🌐
Cplusplus
cplusplus.com › reference › cmath › round
Round
double round (double x); float round (float x);long double round (long double x); double round (T x); // additional overloads for integral types ... Returns the integral value that is nearest to x, with halfway cases rounded away from zero.
🌐
GNU
gnu.org › software › libc › manual › html_node › Rounding-Functions.html
Rounding Functions (The GNU C Library)
The functions listed here perform operations such as rounding and truncation of floating-point values. Some of these functions convert floating point numbers to integer values. They are all declared in math.h.
🌐
Codecogs
codecogs.com › library › computing › c › math.h › round.php
round - Math.h - C - C++ Computing Reference with Worked Examples
The lround and llround functions return the integral value nearest to x (rounding half-way cases away from zero, regardless of the current rounding direction) in the return formats specified. If the rounded value is outside the range of the return type, the numeric result is unspecified and the invalid floating-point exception is raised. A range error may occur if the magnitude of x is too large. ... #include <stdio.h> #include <math.h> int main(void) { for(double a=120;a<=130;a+=1.0) /* note: increments by fraction are not exact!
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › rounding in c
r/C_Programming on Reddit: Rounding in C
May 8, 2024 -

I have a question when it comes to rounding in C. Does it round up or down at .5? If it does round up, then does that mean that the smallest value of k in the code below can only be 1?

 int main()
{
    int k = 13;
    int i;
    for (i = 0; i < 8; i++) {
        printf("%d", (k%2));
        k >>= 1;
    }
    printf("%n");
}

🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › round-roundf-roundl
round, roundf, roundl | Microsoft Learn
===== Round a float roundf(2.5) is 3 roundf(-2.5) is -3 ===== Round a double round(2.499999900000000163657887242152355611324310302734375) is 2 round(-2.499999900000000163657887242152355611324310302734375) is -2 ===== Round a long double roundl(2.499999900000000163657887242152355611324310302734375) is 2 roundl(-2.499999900000000163657887242152355611324310302734375) is -2 · Math and floating-point support ceil, ceilf, ceill floor, floorf, floorl fmod, fmodf lrint, lrintf, lrintl, llrint, llrintf, llrintl lround, lroundf, lroundl, llround, llroundf, llroundl nearbyint, nearbyintf, nearbyintl rint, rintf, rintl
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – round() function
C round() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function. If decimal value is from ”.1 to .5″, it returns integer value less than the argument. If decimal value is from “.6 to .9″, it returns the integer value greater ...
🌐
CS50
manual.cs50.io › 3 › round
round - CS50 Manual Pages
#include <math.h> double round(double x); float roundf(float x); long double roundl(long double x); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): round(), roundf(), roundl(): _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L · This function rounds x to the nearest integer.
🌐
Tutorial Gateway
tutorialgateway.org › c-round-function
C round function
April 5, 2025 - In this C Programming example, we are allowing the user to enter their own value. Next, this program uses the math round function to find the rounded (nearest) value.
🌐
Delft Stack
delftstack.com › home › howto › round function in c
The Round Function in C | Delft Stack
March 12, 2025 - The round function in C rounds a floating-point number to the nearest integer. Do I need to include any special libraries to use the round function? Yes, you need to include the math.h library to use the round function.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re199.html
round C99 - C in a Nutshell [Book]
December 16, 2005 - Process Control16.11.1. Communication with the Operating System16.11.2. Signals16.12. Internationalization16.13. Nonlocal Jumps16.14. Debugging16.15. Error Messages ... #include <math.h> doubleround( double x ); float roundf( float x ); long double roundl( long double x );
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 98999-rounding-c.html
Rounding in C
C99 has a function called round() that you can get if you #include <math.h>. Otherwise floor(x+0.5) will also work (note: the ceil technique will round 4.5 -> 4, but it will work for every number strictly greater than a half-integer). You just have to pick one, it doesn't matter.
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › ceil
C math.h ceil() - Round Up to Integer | Vultr Docs
September 27, 2024 - In C programming, rounding numbers is a common requirement, whether for ensuring proper allocation of resources, setting bounds, or in computation where precision matters but integer values are needed. The math.h library provides several functions to handle such tasks, with ceil() being specifically used to round up floating point numbers to the nearest integer.