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


当前回答

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

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

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

其他回答

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

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

数学。地板滑到左边… 数学。细胞向右滑动… 数学。截断criiiiss croooss(地板/天花板始终朝向0) 数学。圆润恰恰,非常流畅……(走到最近的一边)

让我们开始工作吧!(⌐□_□)

向左……Math.floor 你们现在都收回来……-- 这次跳了两下……- = 2

大家鼓掌✋✋

你能走多低?你能往下走吗?一直到地板上?

if (this == "wrong")
    return "i don't wanna be right";

Math.truncate(x)也与int(x)相同。 通过去掉一个正的或负的分数,你总是趋向0。

根据Floor的数学定义,即“小于或等于一个数字的最大整数”,这是完全明确的,而Truncate只是删除小数部分,这相当于四舍五入到0。

试试这个。

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

Truncate去掉小数点。