且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

舍入到第二个小数点

更新时间:2023-12-03 16:40:22

您可以乘以100,然后舍入为整数。

You can multiply by 100 and then round to an integer. Then put the decimal point after the first 2 digits.

例如:

void round(double x)
{
   double y = 100 * x;
   int rounded = (int)(y + 0.5);
   printf("%lf rounded = %d.%02d\n", x, rounded / 100, rounded % 100);
}