tags:

views:

3497

answers:

4

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

float conver= 45.592346543;

Would like to round actual value to one decimal place. conver = 45.6

Thanks.

+1  A: 
#include "math.h" // can't input less than and greater than :)

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

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

João da Silva
that round to the nearest integer...not what he asked for.
Evan Teran
that's right, I got ahead of myself. I up voted Eduard's answer.
João da Silva
+7  A: 

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

Eduard - Gabriel Munteanu
Nice, everyone else ignored the fact that the asker didn't ask to round to nearest integer. It should be noted that because of imprecision in floating point. When printing you will likely see "45.59999" given the example.
Evan Teran
A nice more general solution would be: double f(double x, int decimal_points) { int n = pow(10, decimal_points); return roundf(n * x) / n; }
Evan Teran
unresolved external symbol _roundf referenced in function _f have included math.h but it doesn't like roundf() I am in VS .NET, is foundf only for linux?
Tommy
Tommy, roundf() is defined in C99, so every compliant compiler should support it. Perhaps you're not linking with the math library?
Eduard - Gabriel Munteanu
I don't think the newer visual studio's made any effort to support C99.
Evan Teran
You could just use this though: floor((10 * x) + 0.5) / 10;
Evan Teran
A: 

Just to generalize Rob's answer a little, if you're not doing it on output, you can still use the same interface with sprintf().

I think there is another way to do it, though. You can try ceil() and floor() to round up and down. A nice trick is to add 0.5, so anything over 0.5 rounds up but anything under it rounds down. ceil() and floor() only work on doubles though.

EDIT: Also, for floats, you can use truncf() to truncate floats. The same +0.5 trick should work to do accurate rounding.

Chris Lutz
+5  A: 

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.

Matt J
Rounds down seems to work OK but for example rounding 45.569346543; is 45.599998....or 45.5 with *1.0f....I'm closer thought, need to read floor and ceil again. thanks guys.
Tommy
It is coming from floating point inaccuracy, I changed to double, works great. Thanks.
Tommy