Math.Floor()和Math.Truncate()之间的区别

时间:2008-08-01 00:59:11

标签: .net math

.NET中Math.Floor()Math.Truncate()之间有什么区别?

13 个答案:

答案 0 :(得分:456)

Math.Floor向下舍入,Math.Ceiling向上舍入,Math.Truncate向下舍入为零。因此,对于正数,Math.TruncateMath.Floor类似,对于负数,Math.Ceiling类似Math.Round。这是reference

为了完整性,{{1}}舍入到最接近的整数。如果数字正好位于两个整数之间,那么它将朝向偶数。 Reference.

另见:Pax Diablo's answer。强烈推荐!

答案 1 :(得分:367)

请按照以下链接获取MSDN描述:

  • Math.Floor,向下舍入负无穷大。
  • Math.Ceiling,向正无穷大方向前进。
  • Math.Truncate,向上或向下舍入为零。
  • Math.Round,它会舍入到最接近的整数或指定的小数位数。如果行为在两种可能性之间完全等距,则可以指定行为,例如舍入以使最后一位数为偶数(“Round(2.5,MidpointRounding.ToEven)”变为2)或者使其远离零(“Round(2.5,MidpointRounding.AwayFromZero)”成为3)。

以下图表和表格可能有所帮助:

-3        -2        -1         0         1         2         3
 +--|------+---------+----|----+--|------+----|----+-------|-+
    a                     b       c           d            e

                       a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8
                       ======  ======  =====  =====  =====
Floor                    -3      -1      0      1      2
Ceiling                  -2       0      1      2      3
Truncate                 -2       0      0      1      2
Round (ToEven)           -3       0      0      2      3
Round (AwayFromZero)     -3      -1      0      2      3

请注意,Round比看起来强大得多,只是因为它可以舍入到特定的小数位数。所有其他的总是小数点零。例如:

n = 3.145;
a = System.Math.Round (n, 2, MidpointRounding.ToEven);       // 3.14
b = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15

使用其他函数,你必须使用multiply / divide trickery来达到同样的效果:

c = System.Math.Truncate (n * 100) / 100;                    // 3.14
d = System.Math.Ceiling (n * 100) / 100;                     // 3.15

答案 2 :(得分:55)

Math.Floor()向负无穷大方向发展

Math.Truncate向上或向下舍入为零。

例如:

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

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

答案 3 :(得分:42)

一些例子:

Round(1.5) = 2
Round(2.5) = 2
Round(1.5, MidpointRounding.AwayFromZero) = 2
Round(2.5, MidpointRounding.AwayFromZero) = 3
Round(1.55, 1) = 1.6
Round(1.65, 1) = 1.6
Round(1.55, 1, MidpointRounding.AwayFromZero) = 1.6
Round(1.65, 1, MidpointRounding.AwayFromZero) = 1.7

Truncate(2.10) = 2
Truncate(2.00) = 2
Truncate(1.90) = 1
Truncate(1.80) = 1

答案 4 :(得分:23)

它们在功能上等同于正数。不同之处在于他们如何处理负数。

例如:

Math.Floor(2.5) = 2
Math.Truncate(2.5) = 2

Math.Floor(-2.5) = -3
Math.Truncate(-2.5) = -2

MSDN链接:   - Math.Floor Method   - Math.Truncate Method

P.S。谨防Math.Round它可能不是你所期望的。

获得"标准"舍入结果使用:

float myFloat = 4.5;
Console.WriteLine( Math.Round(myFloat) ); // writes 4
Console.WriteLine( Math.Round(myFloat, 0, MidpointRounding.AwayFromZero) ) //writes 5
Console.WriteLine( myFloat.ToString("F0") ); // writes 5

答案 5 :(得分:21)

Math.Floor()轮 “向负无穷大”,符合IEEE Standard 754第4节。

Math.Truncate()向“最接近零的整数”舍入。

答案 6 :(得分:19)

<强> math.floor()

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

MSDN system.math.floor

<强> math.truncate()

计算数字的整数部分。

MSDN system.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

答案 7 :(得分:14)

Math.floor左边的sliiiide ...
Math.ceil sliiiide在右边...
Math.truncate criiiiss crooooss(地板/ ceil始终朝向0)
Math.round cha cha,真正顺利......(转到最近的一侧)

让我们去上班吧! (⌐□□_)

在左边...... Math.floor
现在把它拿回来所有...... --
这次有两次跳跃...... -=2

每个人都拍手✋✋

你能走多远?你可以低调吗?一直到floor

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

Math.truncate(x)也与int(x)相同 通过删除正或负分数,您总是朝向0。

答案 8 :(得分:13)

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

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

答案 9 :(得分:2)

Mat.floor()将始终四舍五入,即返回LESSER整数。尽管round()将返回NEAREST整数

答案 10 :(得分:1)

Math.Floor():

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

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

Math.Truncate():

删除数字的小数位并替换为零

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

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

答案 11 :(得分:0)

截断会删除小数点。

答案 12 :(得分:0)

按照Floor的数学定义,即“小于或等于一个数字的最大整数”,这是完全明确的,而Truncate只是删除了小数部分,相当于向0舍入。 >