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
๐ŸŒ
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.
๐ŸŒ
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....
Discussions

math - Concise way to implement round() in C? - Stack Overflow
The embedded C I'm using doesn't have a round() function it it's math lib, what would be a concise way to implement this in C? I was thinking of printing it to a string, looking for the decimal pl... More on stackoverflow.com
๐ŸŒ stackoverflow.com
rounding - Using round() function in c - Stack Overflow
I'm a bit confused about the round() function in C. First of all, man says: SYNOPSIS #include double round(double x); RETURN VALUE These functions return the rounded inte... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Rounding in C
Have you considered simply trying the code out? More on reddit.com
๐ŸŒ r/C_Programming
37
0
May 8, 2024
How to round to the nearest integer?
You can safely cast the results of round or roundf to long. C also provides a function that does this for you, lround and lroundf. The round functions return the nearest integer in float format, it will not return something like 4.9999999, it will return a number that represents 5 exacts. Except... Floating point is weird, because of course it is. Floating point can't represent all integers exactly, and larger than positive or negative 224 for 32-bit floats and 253 for 64-bit doubles present problems. If your numbers are within a reasonable range then the above holds true. If your numbers are outside this range then unpredictable things may occur. More on reddit.com
๐ŸŒ r/C_Programming
16
16
January 13, 2024
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_round.htm
C Standard Library: round Function
The C library round() function can be used to calculate the floating-point into the nearest integer. This function is a part of C99 standard and defined under the header math.h.
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);
}
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ round-roundf-roundl
round, roundf, roundl | Microsoft Learn
July 9, 2025 - Rounds a floating-point value to ... round(X) // Requires C11 or later ... The round functions return a floating-point value that represents the nearest integer to x....
๐ŸŒ
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....
๐ŸŒ
Fresh2Refresh
fresh2refresh.com โ€บ home โ€บ c programming tutorial โ€บ c โ€“ arithmetic functions โ€บ c โ€“ round() function
C round() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - C round() function:round( ) function in C returns the nearest integer value of the float/double/long double argument passed to this function.
Find elsewhere
Top answer
1 of 3
6

The wording in the man-page is meant to be read literally, that is in its mathematical sense. The wording "x is integral" means that x is an element of Z, not that x has the data type int.

Casting a double to int can be dangerous because the maximum arbitrary integral value a double can hold is 2^52 (assuming an IEEE 754 conforming binary64 ), the maximum value an int can hold might be smaller (it is mostly 32 bit on 32-bit architectures and also 32-bit on some 64-bit architectures).

If you need only powers of ten you can test it with this little program yourself:

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

int main(){
  int i;
  for(i = 0;i < 26;i++){
    printf("%d:\t%.2f\t%d\n",i, pow(10,i), (int)pow(10,i));
  }
  exit(EXIT_SUCCESS);
}

Instead of casting you should use the functions that return a proper integral data type like e.g.: lround(3).

2 of 3
4

here is an excerpt from the man page.

   #include <math.h>

   double round(double x);
   float roundf(float x);
   long double roundl(long double x);

notice: the returned value is NEVER a integer. However, the fractional part of the returned value is set to 0.

notice: depending on exactly which function is called will determine the type of the returned value.

Here is an excerpt from the man page about which way the rounding will be done:

   These functions round x to the nearest integer, but round halfway cases
   away  from  zero  (regardless  of  the  current rounding direction, see
   fenv(3)), instead of to the nearest even integer like rint(3).

   For example, round(0.5) is 1.0, and round(-0.5) is -1.0.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ round-function-c
round function in C โ€“ Linux Hint
The round( ) function in the C programming language provides the integer value that is nearest to the float, the double or long double type argument passed to it. If the decimal number is between โ€œ1 and.5โ€ฒโ€ฒ, it gives an integer number less than the argument.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-round-function
C round function
April 5, 2025 - In this program, We are going to find the closest integer value of different numbers and display the output. #include <stdio.h> #include <math.h> int main() { printf("\n The Round Value of 0.75 = %.2f ", round(0.75)); printf("\n The Round Value of 15.25 = %.2f ", round(15.25)); printf("\n The Round Value of 152.50 = %.2f ", round(152.50)); printf("\n The Round Value of -14.36 = %.2f ", round(-14.36)); printf("\n The Round Value of -26.82 = %.2f ", round(-26.32)); printf("\n The Round Value of -90.50 = %.2f \n", round(-90.50)); return 0; }
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cmath โ€บ round
Round
Returns the integral value that is nearest to x, with halfway cases rounded away from zero. Header <tgmath.h> provides a type-generic macro version of this function.
๐ŸŒ
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");
}

๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ round
round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl - cppreference.com
May 23, 2024 - 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.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ libc โ€บ manual โ€บ html_node โ€บ Rounding-Functions.html
Rounding Functions (The GNU C Library)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts. These functions round x downwards to the nearest integer, returning that value as a double.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ round function in c
The Round Function in C | Delft Stack
March 12, 2025 - The primary purpose of this function is to round a floating-point number to the nearest integer. If the fractional part of the number is 0.5 or greater, it rounds up to the next integer.
๐ŸŒ
CodeToFun
codetofun.com โ€บ c โ€บ math-round
C round() Function | CodeToFun
November 16, 2024 - The round() function is a standard library function in C that is used to round a floating-point value to the nearest integer, using the current rounding mode.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1428917 โ€บ how-to-use-round-function-in-c-language
How to use round function in c language? | Sololearn: Learn to code for FREE!
July 29, 2018 - Hi Kari, When rounding floating point numbers in C and other languages you have many options, like round down, round up, round to the nearest integer, etc. In the library math.h in C you can find: floor: round down. ceil: round up. round: round to nearest integer. You can find an explanation of these functions, as well as code examples in this site: http://www.cplusplus.com/reference/cmath/round/ Good coding.