可能重复:
Round a double to 2 significant figures after decimal point如何四舍五入到特定的小数精度?
我有一个值var i = 0.69999980926513672
。我需要将此值舍入到0.7
是否有内置方法可以执行此操作?
可能重复:
Round a double to 2 significant figures after decimal point如何四舍五入到特定的小数精度?
我有一个值var i = 0.69999980926513672
。我需要将此值舍入到0.7
是否有内置方法可以执行此操作?
您正在寻找Math.Round
方法。
//first param is number to round
//second param is the accuracy to use in the rounding (number of decimal places)
Math.Round(i, 2)
使用Math.Round方法:
double i = 0.69999980926513672;
double result = Math.Round(i, 2);
用途之一:
System.Math.Round (i, 1, MidpointRounding.ToEven);
System.Math.Round (i, 1, MidpointRounding.AwayFromZero);
所不同的是它如何处理是等距离的舍入点数(例如,0.65在您的案件既可以去到0.7或0.6)。
这是一个answer我给了另一个持有更多信息的问题。
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));
- 编辑
哇,你眨眼还有其他5个答案!
这是Stack Overflow上最常见的“数学”问题之一。 – 2009-09-08 06:48:10
充分利用您丰富的答案组合+1 – 2009-09-08 06:50:16
正如我的妻子所说,当我全速飞行时,从谈话的角度讲,越少人被迫听我说话,他们似乎变得越快乐:-)所以这可能是最好的保持简短的答案,并参考我的链接更多的大量散文。 – paxdiablo 2009-09-08 06:59:58