Math.Floor()和Math.Truncate()之间的区别
 .NET中的Math.Floor()和Math.Truncate()什么区别? 
  Math.Floor向下Math.Ceiling , Math.Ceiling向上Math.Truncate , Math.Truncate向零Math.Truncate 。 因此, Math.Truncate就像Math.Floor用于正数,而Math.Ceiling用于负数。 这是参考 。 
 为了完整性, Math.Round四舍五入到最接近的整数。 如果这个数字恰好在两个整数的中间,那么它就会朝向那个整数。  参考。 
另见: Pax Diablo的回答 。 强烈推荐!
请按以下链接查看以下MSDN描述:
-   Math.Floor,向着无穷大的方向发展。
-   Math.Ceiling,正向无穷大迈进。
-   Math.Truncate,向上Math.Truncate到零。
-   Math.Round,其四舍五入到最接近的整数或指定的小数位数。 你可以指定行为,如果它在两个可能性之间是完全等距的,比如四舍五入,那么最后的数字是偶数(“Round(2.5,MidpointRounding.ToEven)”变成2),或者它离零更远(“Round(2.5,MidpointRounding.AwayFromZero)“成为3)。
下图和表格可能有所帮助:
 -3 -2 -1 0 1 2 3 +--|------+---------+----|----+--|------+----|----+-------|-+ abcde 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 
与其他function,你必须使用乘法/除法,以达到相同的效果:
 c = System.Math.Truncate (n * 100) / 100; // 3.14 d = System.Math.Ceiling (n * 100) / 100; // 3.15 
一些例子:
 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()向负无穷 
  Math.Truncate向上或向下Math.Truncate为零。 
例如:
 Math.Floor(-3.4) = -4 Math.Truncate(-3.4) = -3 
而
 Math.Floor(3.4) = 3 Math.Truncate(3.4) = 3 
 根据IEEE标准754第4节, Math.Floor()向“负无穷大”方向发展。 
  Math.Truncate()四舍五入到最接近零的整数。 
它们在function上等同于正数。 不同之处在于他们如何处理负数。
例如:
 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方法
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() 
返回小于或等于指定数字的最大整数。
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 
  Math.Floor() :返回小于或等于指定的双精度浮点数的最大整数。 
  Math.Round() :将值四舍五入到最接近的整数或指定的小数位数。