.NET 中的 Math.Floor()
和 Math.Truncate()
有什么区别?
Math.Floor
向下取整,Math.Ceiling
向上取整,Math.Truncate
向零取整。因此,对于正数,Math.Truncate
类似于 Math.Floor
,对于负数类似于 Math.Ceiling
。这是reference。
为了完整起见,Math.Round
舍入到最接近的整数。如果该数字正好在两个整数之间,那么它会朝偶数四舍五入。 Reference.
另请参阅:Pax Diablo's answer。强烈推荐!
按照以下链接获取 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
对于其他功能,您必须使用乘法/除法技巧来达到相同的效果:
c = System.Math.Truncate (n * 100) / 100; // 3.14
d = System.Math.Ceiling (n * 100) / 100; // 3.15
even
视为舍入数字中最后一个 digit 的属性,而不是意味着整个 number 必须是二的倍数。顺便说一句,很抱歉花了这么长时间才回复你,希望你不只是坐等我的回复:-)
Math.Floor()
向负无穷舍入
Math.Truncate
向上或向下舍入到零。
例如:
Math.Floor(-3.4) = -4
Math.Truncate(-3.4) = -3
尽管
Math.Floor(3.4) = 3
Math.Truncate(3.4) = 3
Math.floor
向左滑动...
Math.ceil
向右滑动...
Math.truncate
criiiiss crooooss(地板/天花板始终朝向 0)
Math.round
恰恰,非常光滑...(到最近的一侧)
咱们上班去! (⌐□_□)
向左……Math.floor
你们现在把它拿回来……--
这次跳两跳……-=2
大家拍手✋✋
你能走多低?能低点吗?一直到 floor
?
if (this == "wrong")
return "i don't wanna be right";
Math.truncate(x)
也与 int(x)
相同。
通过删除正分数或负分数,您始终会趋向 0。
一些例子:
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
它们在功能上与正数等价。不同之处在于它们如何处理负数。
例如:
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
PS 当心 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
试试这个,例子:
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
还有 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()
返回小于或等于指定数字的最大整数。微软system.math.floor
math.truncate()
计算数字的整数部分。微软system.math.truncate
Math.Floor()
:返回小于或等于指定双精度浮点数的最大整数。
Math.Round()
:将值四舍五入为最接近的整数或指定的小数位数。
Floor()
和 Truncate()
之间的区别,而不是 Floor()
和 Round()
。
Math.floor()
将始终向下舍入,即返回 LESSER 整数。而 round()
将返回 NEAREST 整数
数学.地板()
返回小于或等于指定数字的最大整数。
数学.截断()
计算数字的整数部分。
Math.Floor() :
它给出小于或等于给定数字的最大整数。
Math.Floor(3.45) =3
Math.Floor(-3.45) =-4
数学.截断():
它删除数字的小数位并用零替换
Math.Truncate(3.45)=3
Math.Truncate(-3.45)=-3
同样从上面的例子我们可以看到对于正数来说 floor 和 truncate 是相同的。
截断删除小数点。
按照 Floor 的数学定义,即“小于或等于一个数的最大整数”,这完全没有歧义,而 Truncate 只是去掉了小数部分,相当于向 0 舍入。
(int)myDouble
何时与(int)Math.Truncate(myDouble)
不同?