.NET中Math.Floor()和Math.Truncate()的区别是什么?


当前回答

Truncate去掉小数点。

其他回答

试试这个。

Math.Floor() vs Math.Truncate()

Math.Floor(2.56) = 2
Math.Floor(3.22) = 3
Math.Floor(-2.56) = -3
Math.Floor(-3.26) = -4

Math.Truncate(2.56) = 2
Math.Truncate(2.00) = 2
Math.Truncate(1.20) = 1
Math.Truncate(-3.26) = -3
Math.Truncate(-3.96) = -3

还Math.Round ()

   Math.Round(1.6) = 2
   Math.Round(-8.56) = -9
   Math.Round(8.16) = 8
   Math.Round(8.50) = 8
   Math.Round(8.51) = 9

math.floor ()

返回小于或等于指定数字的最大整数。 MSDN system.math.floor

math.truncate ()

计算一个数的积分部分。 MSDN system.math.truncate

Math.Floor()舍入到负无穷

数学。向上或向下截断舍入到零。

例如:

Math.Floor(-3.4)     = -4
Math.Truncate(-3.4)  = -3

Math.Floor(3.4)     = 3
Math.Truncate(3.4)  = 3

Math.Floor ():

它给出小于或等于给定数的最大整数。

    Math.Floor(3.45) =3
    Math.Floor(-3.45) =-4

Math.Truncate ():

它删除数字的小数点后几位并替换为零

Math.Truncate(3.45)=3
 Math.Truncate(-3.45)=-3

从上面的例子中我们还可以看到,对于正数,下限和截断是相同的。

Truncate去掉小数点。

数学。楼层四舍五入,马斯。天花板来了,还有数学。将回合截断至零。因此,数学。截断就像数学。正数的地板,和数学一样。负数的上限。这是参考资料。

为了完整起见,请使用数学。四舍五入到最接近的整数。如果这个数字恰好在两个整数的中间,那么它就会四舍五入到偶数。参考。

参见:暗黑和平的答案。强烈推荐!