Rounding Number to 2 Decimal Places in C

2020-01-22 11:19发布

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

18条回答
SAY GOODBYE
2楼-- · 2020-01-22 12:03

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 a good idea, due to the way floating point numbers work: you usually don't want to do equality comparisons for floating point, instead use a target value +/- a sigma value. Or encode the number as a string with a known precision, and compare that.

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.

查看更多
3楼-- · 2020-01-22 12:03

I made this macro for rounding float numbers. Add it in your header / being of file

#define ROUNDF(f, c) (((float)((int)((f) * (c))) / (c)))

Here is an example:

float x = ROUNDF(3.141592, 100)

x equals 3.14 :)

查看更多
干净又极端
4楼-- · 2020-01-22 12:04

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;   /* Result: 37.77 */
float nearest = roundf(val * 100) / 100;  /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100;      /* Result: 37.78 */

Notice that there 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.

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.

For much (much!) more information on rounding, and especially on tie-breaking rules for rounding to nearest, see the Wikipedia article on Rounding.

查看更多
我只想做你的唯一
5楼-- · 2020-01-22 12:10

In C++ (or in C with C-style casts), you could create the function:

/* Function to control # of decimal places to be output for x */
double showDecimals(const double& x, const int& numDecimals) {
    int y=x;
    double z=x-y;
    double m=pow(10,numDecimals);
    double q=z*m;
    double r=round(q);

    return static_cast<double>(y)+(1.0/m)*r;
}

Then std::cout << showDecimals(37.777779,2); would produce: 37.78.

Obviously you don't really need to create all 5 variables in that function, but I leave them there so you can see the logic. There are probably simpler solutions, but this works well for me--especially since it allows me to adjust the number of digits after the decimal place as I need.

查看更多
beautiful°
6楼-- · 2020-01-22 12:14

this function takes the number and precision and returns the rounded off number

float roundoff(float num,int precision)
{
      int temp=(int )(num*pow(10,precision));
      int num1=num*pow(10,precision+1);
      temp*=10;
      temp+=5;
      if(num1>=temp)
              num1+=10;
      num1/=10;
      num1*=10;
      num=num1/pow(10,precision+1);
      return num;
}

it converts the floating point number into int by left shifting the point and checking for the greater than five condition.

查看更多
你好瞎i
7楼-- · 2020-01-22 12:16
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);
查看更多
登录 后发表回答