Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to round a float in C or do I need to write my own?

Tags:

Is there a function to round a float in C or do I need to write my own?

float conver = 45.592346543;

I would like to round the actual value to one decimal place, conver = 45.6.

like image 517
T.T.T. Avatar asked Jan 30 '09 20:01

T.T.T.


People also ask

Is there a rounding function in C?

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.

How do you round off a float?

Round() Round() is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

How do you round a floating point number to the nearest integer in C?

Use the lround Function to Round Floating-Point Number to the Nearest Integer and Return Integral Type. The lround function, on the other hand, round to the nearest in integer and return the integral value.


3 Answers

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:

#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:

#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.

like image 200
Matt J Avatar answered Jan 01 '23 13:01

Matt J


Sure, you can use roundf(). If you want to round to one decimal, then you could do something like: roundf(10 * x) / 10

like image 25
Eduard - Gabriel Munteanu Avatar answered Jan 01 '23 13:01

Eduard - Gabriel Munteanu


#include <math.h>

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

Don't forget to link with -lm. See also ceil(), floor() and trunc().

like image 43
Joao da Silva Avatar answered Jan 01 '23 13:01

Joao da Silva