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


当前回答

Truncate去掉小数点。

其他回答

Truncate去掉小数点。

Math.Floor():返回小于或等于指定的双精度浮点数的最大整数。

round():将值舍入为最接近的整数或指定的小数位数。

试试这个。

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()将始终向下舍入。,返回LESSER整数。While round()将返回NEAREST整数

math.floor ()

返回小于或等于指定数字的最大整数。

math.truncate ()

计算一个数的积分部分。