Math.Floor()
和Math.Truncate()
在.NET中有什么区别?
Math.Floor
方法向下取整,Math.Ceiling
方法向上取整,Math.Truncate
方法向零取整。因此,对于正数而言,Math.Truncate
的作用类似于Math.Floor
,而对于负数而言,Math.Truncate
的作用类似于Math.Ceiling
。参考文献。
为了完备性,Math.Round
方法将数字四舍五入到最接近的整数。如果该数字恰好处于两个整数之间,则它会朝偶数方向舍入。参考文献。
另请参见:Pax Diablo的回答。强烈推荐!
myDouble
为负数时,(int)myDouble
会向零舍入,而(int)Math.Truncate(myDouble)
会朝着更接近零的方向舍入。例如,对于值-1.5,(int)-1.5
将返回-1,而(int)Math.Truncate(-1.5)
将返回-2。 - mpen请参阅以下链接,了解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
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
穿梭来去 (floor/ceil 总是朝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方法 - Math.Truncate方法
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
请尝试这个,示例:
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()
返回小于或等于指定数字的最大整数。 MSDN system.math.floor
math.truncate()
计算数字的整数部分。 MSDN system.math.truncate
Math.Floor()
: 返回小于或等于指定双精度浮点数的最大整数。
Math.Round()
: 将值四舍五入到最接近的整数或指定数量的小数位数。
Floor()
和 Truncate()
的区别,而不是 Floor()
和 Round()
的区别。 - Robert ColumbiaMath.floor()
会将数字向下取整,即返回小于等于该数字的最大整数。而 round()
则是四舍五入后返回最接近的整数。
math.floor()
返回小于或等于指定数字的最大整数。
math.truncate()
计算一个数的整数部分。