.net Math.Floor() 和 Math.Truncate() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:35:23  来源:igfitidea点击:

Difference between Math.Floor() and Math.Truncate()

.netmath

提问by Anonymous User

What is the difference between Math.Floor()and Math.Truncate()in .NET?

.NETMath.Floor()Math.Truncate().NET 有什么区别?

回答by Chris Jester-Young

Math.Floorrounds down, Math.Ceilingrounds up, and Math.Truncaterounds towards zero. Thus, Math.Truncateis like Math.Floorfor positive numbers, and like Math.Ceilingfor negative numbers. Here's the reference.

Math.Floor向下Math.Ceiling舍入、向上Math.Truncate舍入和向零舍入。因此,Math.Truncate就像Math.Floor正数一样,就像Math.Ceiling负数一样。这是参考

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

为完整Math.Round起见,四舍五入到最接近的整数。如果该数字正好位于两个整数的中间,则它会向偶数四舍五入。参考。

See also: Pax Diablo's answer. Highly recommended!

另请参阅:Pax Diablo 的回答。强烈推荐!

回答by paxdiablo

Follow these links for the MSDN descriptions of:

请按照以下链接获取 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).
  • Math.Floor,向下舍入到负无穷大。
  • Math.Ceiling,向上取整为正无穷大。
  • Math.Truncate,向上或向下舍入到零。
  • Math.Round,四舍五入到最接近的整数或指定的小数位数。如果它在两种可能性之间完全等距,您可以指定行为,例如四舍五入以使最后一位数字为偶数(“ Round(2.5,MidpointRounding.ToEven)” 变为 2)或使其远离零(“ Round(2.5,MidpointRounding.AwayFromZero)”变为 3)。

The following diagram and table may help:

下面的图表和表格可能会有所帮助:

-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

Note that Roundis a lot more powerful than it seems, simply because it can round to a specific number of decimal places. All the others round to zero decimals always. For example:

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

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

With the other functions, you have to use multiply/divide trickery to achieve the same effect:

对于其他函数,您必须使用乘法/除法技巧才能达到相同的效果:

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

回答by Azhar

Math.Floor()rounds toward negative infinity

Math.Floor()向负无穷大舍入

Math.Truncaterounds up or down towards zero.

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

For example:

例如:

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

while

尽管

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

回答by Marek Grzenkowicz

Some examples:

一些例子:

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

回答by Puddle

Math.floorsliiiide to the left...
Math.ceilsliiiide to the right...
Math.truncatecriiiiss crooooss (floor/ceil always towards 0)
Math.roundcha cha, real smooth... (go to closest side)

Math.floorsliiiide 向左...
Math.ceilsliiiide 向右...
Math.truncatecriiiiss crooooss(地板/天花板始终朝向 0)
Math.roundcha cha,真正平滑...(转到最近的一侧)

Let's go to work! (?□_□)

我们去上班吧!(?□_□)

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

向左……Math.floor
现在把它拿回来……--
这次跳两步……-=2

Everybody clap your hands ??

大家拍拍手??

How low can you go? Can you go down low? All the way to the floor?

你能走多低?你能低下吗?一路到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.

Math.truncate(x)也一样int(x)
通过去除一个正数或负数,你总是朝着 0 前进。

回答by Sandesh

They are functionally equivalent with positive numbers. The difference is in how they handle negative numbers.

它们在功能上与正数等效。不同之处在于它们处理负数的方式。

For example:

例如:

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

MSDN 链接: - Math.Floor 方法- Math.Truncate 方法

P.S. Beware of Math.Round it may not be what you expect.

PS 当心 Math.Round 它可能不是你所期望的。

To get the "standard" rounding result use:

要获得“标准”舍入结果,请使用:

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

回答by safin chacko

Try this, Examples:

试试这个,例子:

Math.Floor()vs Math.Truncate()

Math.Floor()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

Also Math.Round()

还有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()

math.floor()

Returns the largest integer less than or equal to the specified number. MSDN system.math.floor

返回小于或等于指定数字的最大整数。MSDN system.math.floor

math.truncate()

math.truncate()

Calculates the integral part of a number. MSDN system.math.truncate

计算一个数的整数部分。MSDN system.math.truncate

回答by safin chacko

Math.Floor()rounds "toward negative infinity" in compliance to IEEE Standard 754section 4.

Math.Floor()符合IEEE 标准 754第 4 节的“向负无穷大”舍入

Math.Truncate()rounds " to the nearest integer towards zero."

Math.Truncate()将“四舍五入到接近零的最接近的整数。”

回答by Pinky

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

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

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

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

回答by vidy

Math.floor()will always round down ie., it returns LESSER integer. While round()will return the NEAREST integer

Math.floor()将始终向下舍入,即,它返回较小的整数。Whileround()将返回最近的整数

math.floor()

数学地板()

Returns the largest integer less than or equal to the specified number.

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

math.truncate()

math.truncate()

Calculates the integral part of a number.

计算一个数的整数部分。