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


当前回答

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

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

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

大家鼓掌✋✋

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

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

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

其他回答

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

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

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

大家鼓掌✋✋

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

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

Math.truncate(x)也与int(x)相同。 通过去掉一个正的或负的分数,你总是趋向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

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去掉小数点。

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