【发布时间】:2019-10-22 15:21:18
【问题描述】:
我尝试过使用 Math.Round 和 MidpointRounding。这似乎不能满足我的需要。
例子:
52.34567 rounded to 2 decimals UP = 52.35
1.183 rounded to 2 decimals DOWN = 1.18
我需要编写自定义函数吗?
编辑:
我应该更具体的。
有时我需要一个像 23.567 这样的数字来向下舍入到 23.56。 在这种情况下...
Math.Round(dec, 2, MidpointRounding.AwayFromZero) gives 23.57
Math.Round(dec, 2, MidpointRounding.ToEven) gives 23.57
最多可以输出小数点后 9 位的小数,需要四舍五入到小数点后 1、2、3 甚至 4 位。
【问题讨论】:
-
您是否尝试过乘以 100、四舍五入并除以 100?
-
你能显示你用来进行四舍五入的代码吗?
-
这似乎对我有用。也许我误解了这个问题?
Response.Write(Math.Round(52.34567, 2).ToString());输出:52.35 -
显然他没有尝试过,因为它完全符合他的需要。他甚至可以自己测试:
Console.WriteLine(Math.Round(52.34567, 2));和Console.WriteLine(Math.Round(1.183, 2));