As Rob mentioned, you probably just want to print the float to 1 decimal place. In this case, you can do something like the following:

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

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);
  return 0;
}

If you want to actually round the stored value, that's a little more complicated. For one, your one-decimal-place representation will rarely have an exact analog in floating-point. If you just want to get as close as possible, something like this might do the trick:

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

int main()
{
  float conver = 45.592346543;
  printf("conver is %0.1f\n",conver);

  conver = conver*10.0f;
  conver = (conver > (floor(conver)+0.5f)) ? ceil(conver) : floor(conver);
  conver = conver/10.0f;

  //If you're using C99 or better, rather than ANSI C/C89/C90, the following will also work.
  //conver = roundf(conver*10.0f)/10.0f;

  printf("conver is now %f\n",conver);
  return 0;
}

I doubt this second example is what you're looking for, but I included it for completeness. If you do require representing your numbers in this way internally, and not just on output, consider using a fixed-point representation instead.

Answer from Matt J 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.
๐ŸŒ
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....
๐ŸŒ
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
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ round-roundf-roundl
round, roundf, roundl | Microsoft Learn
The round functions return a floating-point value that represents the nearest integer to x. Halfway values are rounded away from zero, regardless of the setting of the floating-point rounding mode.
๐ŸŒ
Quora
quora.com โ€บ How-does-one-round-floats-to-the-nearest-integer-in-C
How does one round floats to the nearest integer in C? - Quora
Answer (1 of 4): add 0.5 [code]#include int main(){ int i; i = 12.34; printf("%d\n", i); // 12 i = 12.34 + 0.5; printf("%d\n", i); // 12 i = 45.67; printf("%d\n", i); // 45 i = 45.67 + 0.5; printf("%d\n", i); // 46 } [/code]
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ rounding-and-truncating-numbers-in-c
Rounding and Truncating numbers using math.h in C
November 10, 2019 - If we pass float number as an argument, the syntax is: ... #include <stdio.h> #include <math.h> int main() { printf("lroundf(+2.72) gives = %d\n", lroundf(2.72)); } ... #include <stdio.h> #include <math.h> int main() { printf("lround(+5.81) gives = %d\n", lround(5.81)); } ... #include <stdio.h> #include <math.h> int main() { printf("lroundl(+2.64) gives = %d\n", lroundl(2.64)); } ... If arg has type long double, roundl, lroundl, llroundl is called.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ round
round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl - cppreference.com
The largest representable floating-point values are exact integers in all standard floating-point formats, so round never overflows on its own; however the result may overflow any integer type (including intmax_t), when stored in an integer variable. POSIX specifies that all cases where lround or llround raise FE_INVALID are domain errors.
๐ŸŒ
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");
}

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_round.htm
C 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.
๐ŸŒ
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. The trunc functions round x towards zero to the nearest integer (returned in floating-point format).
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 119573-round-float-2-decimal-places.html
Round float to 2 decimal places
There might be a more elegant way of doing this in C, but you are posting C++ code here in the C forum so I have an idea that might work on both: ... float num = 3.14159; num *= 100; if(num >= 0) num += 0.5; else num -= 0.5; long round = num; num = round; num /= 100;
๐ŸŒ
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.
๐ŸŒ
GNU
gnu.org โ€บ s โ€บ libc โ€บ manual โ€บ html_node โ€บ Rounding-Functions.html
The GNU C Library - GNU Project - Free Software Foundation (FSF)
This glibc manual version 2.43 (latest) is available in the following formats: ยท The manual is available for the following releases of glibc:
Top answer
1 of 16
152

Editor's Note: The following answer provides a simplistic solution that contains several implementation flaws (see Shafik Yaghmour's answer for a full explanation). Note that C++11 includes std::round, std::lround, and std::llround as builtins already.

There's no round() in the C++98 standard library. You can write one yourself though. The following is an implementation of round-half-up:

double round(double d)
{
  return floor(d + 0.5);
}

The probable reason there is no round function in the C++98 standard library is that it can in fact be implemented in different ways. The above is one common way but there are others such as round-to-even, which is less biased and generally better if you're going to do a lot of rounding; it's a bit more complex to implement though.

2 of 16
106

The C++03 standard relies on the C90 standard for what the standard calls the Standard C Library which is covered in the draft C++03 standard (closest publicly available draft standard to C++03 is N1804) section 1.2 Normative references:

The library described in clause 7 of ISO/IEC 9899:1990 and clause 7 of ISO/IEC 9899/Amd.1:1995 is hereinafter called the Standard C Library.1)

If we go to the C documentation for round, lround, llround on cppreference we can see that round and related functions are part of C99 and thus won't be available in C++03 or prior.

In C++11 this changes since C++11 relies on the C99 draft standard for C standard library and therefore provides std::round and for integral return types std::lround, std::llround :

#include <iostream>
#include <cmath>

int main()
{
    std::cout << std::round( 0.4 ) << " " << std::lround( 0.4 ) << " " << std::llround( 0.4 ) << std::endl ;
    std::cout << std::round( 0.5 ) << " " << std::lround( 0.5 ) << " " << std::llround( 0.5 ) << std::endl ;
    std::cout << std::round( 0.6 ) << " " << std::lround( 0.6 ) << " " << std::llround( 0.6 ) << std::endl ;
}

Another option also from C99 would be std::trunc which:

Computes nearest integer not greater in magnitude than arg.

#include <iostream>
#include <cmath>

int main()
{
    std::cout << std::trunc( 0.4 ) << std::endl ;
    std::cout << std::trunc( 0.9 ) << std::endl ;
    std::cout << std::trunc( 1.1 ) << std::endl ;
    
}

If you need to support non C++11 applications your best bet would be to use boost round, iround, lround, llround or boost trunc.

Rolling your own version of round is hard

Rolling your own is probably not worth the effort as Harder than it looks: rounding float to nearest integer, part 1, Rounding float to nearest integer, part 2 and Rounding float to nearest integer, part 3 explain:

For example a common roll your implementation using std::floor and adding 0.5 does not work for all inputs:

double myround(double d)
{
  return std::floor(d + 0.5);
}

One input this will fail for is 0.49999999999999994, (see it live).

Another common implementation involves casting a floating point type to an integral type, which can invoke undefined behavior in the case where the integral part can not be represented in the destination type. We can see this from the draft C++ standard section 4.9 Floating-integral conversions which says (emphasis mine):

A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.[...]

For example:

float myround(float f)
{
  return static_cast<float>( static_cast<unsigned int>( f ) ) ;
}

Given std::numeric_limits<unsigned int>::max() is 4294967295 then the following call:

myround( 4294967296.5f ) 

will cause overflow, (see it live).

We can see how difficult this really is by looking at this answer to Concise way to implement round() in C? which referencing newlibs version of single precision float round. It is a very long function for something which seems simple. It seems unlikely that anyone without intimate knowledge of floating point implementations could correctly implement this function:

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

On the other hand if none of the other solutions are usable newlib could potentially be an option since it is a well tested implementation.

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-perform-rounding-operations-in-c-435192
How to Perform Rounding Operations in C | LabEx
In this lab, you will learn how to perform rounding operations in C programming. You will start by declaring floating-point variables, then explore the use of various rounding functions, such as round(), floor(), and ceil(), to manipulate the values.