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

Math.Floor() 有什么区别? a>和 Math.Truncate()在.NET中?

0
额外 编辑
意见: 20
十年后你真的需要这个吗?大声笑
额外 作者 L_Church,
为什么十年后有一个赏金?已经有很多答案。有什么我在这里失踪?
额外 作者 Puddle,
例如Math.Floor(5.4)= 5 Math.Truncate(5.4)= 5
额外 作者 subramani,

8 答案

Math.Floor rounds down, Math.Ceiling rounds up, and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers. Here's the reference.

For completeness, Math.Round rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference.

另见: Pax Diablo的回答。强烈推荐!

0
额外
Math类中的(int)等于是什么?
额外 作者 Lei Yang,
何时(int)myDouble(int)Math.Truncate(myDouble)不同?
额外 作者 mpen,
@Chris,我建议你修正你对Round的描述,有两种方法可以进行轮回(AwayFromZero和ToEven),它不会四舍五入到最接近的整数,因为它也可以进行小数四舍五入。
额外 作者 paxdiablo,
因此,只需简单地添加一个原始问题 - Math.Truncate和仅将一个小数或二进制转换为int的区别是什么?难道它不仅仅是向零圆?
额外 作者 Noam Gal,

一些例子:

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
0
额外

请按以下链接查看以下MSDN描述:

  • Math.Floor, which rounds down towards negative infinity.
  • Math.Ceiling, which rounds up towards positive infinity.
  • Math.Truncate, which rounds up or down towards zero.
  • Math.Round, which rounds to the nearest integer or specified number of decimal places. You can specify the behavior if it's exactly equidistant between two possibilities, such as rounding so that the final digit is even ("Round(2.5,MidpointRounding.ToEven)" becoming 2) or so that it's further away from zero ("Round(2.5,MidpointRounding.AwayFromZero)" becoming 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

需要注意的是<�代码>圆</代码>是一个很大的威力比现在看来,仅仅是因为它可以全面为十进制场所的具体数量。所有其他人总是舍入零小数。例如:

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

与其他功能,你必须使用乘法/除法欺骗来达到相同的效果:

c = System.Math.Truncate (n * 100) / 100;                    // 3.14
d = System.Math.Ceiling (n * 100) / 100;                     // 3.15
0
额外
谢谢,@dtroy,我从来没有需要使用该模式,并且,虽然我正确地记录了它,如果文本,我完全得到了错误的例子。希望现在已经解决了。
额外 作者 paxdiablo,
@Richiban,把偶数当作最后一个数字的一个属性,并不意味着整个数字必须是整数二。顺便说一句,对不起,花了这么长时间才回到你身边,希望你不只是坐在等待我的回应:-)
额外 作者 paxdiablo,
Pax,我认为你有一个错误:Round(AwayFromZero)-3 -2 1 2 3 Math.Round(-1.2,MidpointRounding.AwayFromZero)== -1 Math.Round(0.3,MidpointRounding.AwayFromZero)== 0.0等。
额外 作者 dtroy,
很抱歉对这样一个老问题发表评论,但我不得不问:你怎么能把“ToEven”舍入到小数点后两位?奇怪,甚至只适用于整数?
额外 作者 Richiban,

Math.Floor() rounds toward negative infinity

Math.Truncate rounds up or down towards zero.

例如:

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

Math.Floor(3.4)     = 3
Math.Truncate(3.4)  = 3
0
额外

Math.floor sliiiide to the left...
Math.ceil sliiiide to the right...
Math.truncate criiiiss crooooss (floor/ceil always towards 0)
Math.round cha cha, real smooth... (go to closest side)

咱们上班去! (?? _?)

To the left... Math.floor
Take it back now y'all... --
Two hops this time... -=2

大家拍手?

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

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

Math.truncate(x) is also the same as int(x).
by removing a positive or negative fraction, you're always heading towards 0.

0
额外

<�代码> 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

In addition 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
0
额外

Math.Floor(): Returns the largest integer less than or equal to the specified double-precision floating-point number.

Math.Round(): Rounds a value to the nearest integer or to the specified number of fractional digits.

0
额外
OP询问了 Floor()Truncate()之间的区别,而不是 Floor() Round()</ code >。
额外 作者 Robert Columbia,

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

例如:

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

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

MSDN links: - Math.Floor Method - Math.Truncate Method

附:小心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
0
额外