你好,我有这段C#代码:
float n = 2.99499989f;
MessageBox.Show("n = " + n.ToString("f2", CultureInfo.InvariantCulture));
以下是C++中的代码:
float n = 2.99499989f;
printf("n = %.2f", n);
第一个输出3.00。
第二个输出2.99。
我不知道这为什么会发生。
更新:
我也尝试了Objective-C的NSLog,输出结果是2.99。
我需要快速修复它,所以我使用了以下方法:
float n = 2.99499989f;
float round = (float)Math.Round(n, 2);
MessageBox.Show("round = " + round.ToString(CultureInfo.InvariantCulture));
这段代码显示2.99,但是在双精度计算中进行了四舍五入。我找不到Math.RoundF函数。