tags:

views:

10482

answers:

7

How can I round a float (such as 37.777779) to two decimal places (37.78) in C?

+9  A: 
printf("%.2f", 37.777779);
Andrew Coleson
*1 I mean +1 lol :)
AraK
+8  A: 
printf("%.2f", 37.777779);

If you want to write to C-string:

char number[24]; // dummy size, you should take care of the size!
sprintf(number, "%.2f", 37.777779);
AraK
@Sinan: Why the edit? @AraK: No, *you* should take care of the size :). Use snprintf().
aib
@aib: I would guess because /**/ are C style comments and the question is tagged for C
Michael Haren
@Michael: So are //.
aib
C89 only allowed /**/-style, C99 introduced support for //-style. Use a lame/old compiler (or force C89 mode) and you'll be unable to use //-style. Having said that, it's 2009, let's consider them both C and C++ style.
Andrew Coleson
+4  A: 

There isn't a way to round a float to another float because the rounded float may not be representable (a limitation of floating-point numbers). For instance, say you round 37.777779 to 37.78, but the nearest representable number is 37.781.

However, you can "round" a float by using a format string function.

Andrew Keeton
This is no different from saying "there's no way to divide two floats and get a float, because the divided result may not be representable," which may be precisely true but is irrelevant. Floats are always inexact, even for something as basic as addition; the assumption is always that what you actually get is "the float that most closely approximates the exact rounded answer".
Brooks Moses
What I meant is that you cannot round a `float` to n decimal places and then expect the result always have n decimal places. You will still get a `float`, just not the one you expected.
Andrew Keeton
+7  A: 

Assuming you're talking about round the value for printing, then Andrew Coleson and AraK's answer are correct:

printf("%.2f", 37.777779);

But note that if you're aiming to round the number to exactly 37.78 for internal use (eg to compare against another value), then this isn't possible, due to the way floating point numbers work.

See the link in Greg Hewgill's answer to a related question, which also covers why you shouldn't use floating point for financial calculations.

therefromhere
Upvoted for addressing what may be the question behind the question (or the question that should have been behind the question!). That's a rather important point.
Brooks Moses
+1  A: 

You can still use:

float ceilf(float x); // don't forget #include <math.h> and link with -lm.

example:

float valueToRound = 37.777779;
float roundedValue = ceilf(valueToRound * 100) / 100;
ZeroCool
This truncates at decimal point (i.e. will produce 37), and he needs to round to two places _after_ the decimal point.
Pavel Minaev
Rounding to two places after the decimal point is a trivial variation, though (but still should be mentioned in the answer; ZeroCool, want to add an edit?):float roundedValue = ceilf(valueToRound * 100.0) / 100.0;
Brooks Moses
Into a state of sleep :)
ZeroCool
+1  A: 

How about this:

float value = 37.777779;
float rounded = ((int)(value * 100 + .5) / 100.0);
Daniil
-1: a) this won't work for negative numbers (ok, the example is positive but still). b) you don't mention that it's impossible to store the exact decimal value in the float
therefromhere
@therefromhere: (a) You're right (b) What is this? A high school test?
Daniil
+9  A: 

If you just want to round the number for output purposes, then the "%.2f" format string is indeed the correct answer. However, if you actually want to round the floating point value for further computation, something like the following works:

#include <math.h>
/* ... */
float val = 37.777779;

float rounded_down = floorf(val * 100) / 100;
float nearest = floorf(val * 100 +  0.5) / 100;
float rounded_up = ceilf(val * 100) / 100;

Notice that are three different rounding rules you might want to choose: round down (ie, truncate after two decimal places), rounded to nearest, and round up. Usually, you want round to nearest. In this case, the three values are 37.77, 37.78, and 37.78, respectively.

As several others have pointed out, due to the quirks of floating point representation, these rounded values may not be exactly the "obvious" decimal values, but they will be very very close.

Dale Hagglund