.NET中Math.Floor()和Math.Truncate()的区别是什么?
当前回答
数学。楼层四舍五入,马斯。天花板来了,还有数学。将回合截断至零。因此,数学。截断就像数学。正数的地板,和数学一样。负数的上限。这是参考资料。
为了完整起见,请使用数学。四舍五入到最接近的整数。如果这个数字恰好在两个整数的中间,那么它就会四舍五入到偶数。参考。
参见:暗黑和平的答案。强烈推荐!
其他回答
Math.Floor ():
它给出小于或等于给定数的最大整数。
Math.Floor(3.45) =3
Math.Floor(-3.45) =-4
Math.Truncate ():
它删除数字的小数点后几位并替换为零
Math.Truncate(3.45)=3
Math.Truncate(-3.45)=-3
从上面的例子中我们还可以看到,对于正数,下限和截断是相同的。
数学。楼层四舍五入,马斯。天花板来了,还有数学。将回合截断至零。因此,数学。截断就像数学。正数的地板,和数学一样。负数的上限。这是参考资料。
为了完整起见,请使用数学。四舍五入到最接近的整数。如果这个数字恰好在两个整数的中间,那么它就会四舍五入到偶数。参考。
参见:暗黑和平的答案。强烈推荐!
Math.Floor()舍入到负无穷
数学。向上或向下截断舍入到零。
例如:
Math.Floor(-3.4) = -4
Math.Truncate(-3.4) = -3
而
Math.Floor(3.4) = 3
Math.Truncate(3.4) = 3
Truncate去掉小数点。
试试这个。
Math.Floor() vs 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
推荐文章
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- AppSettings从.config文件中获取值
- 如何检查IEnumerable是否为空或空?
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- HyperLogLog算法是如何工作的?
- 如何在iis7应用程序池中设置。net Framework 4.5版本
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何指定最小值,但没有使用范围数据注释属性的最大小数?
- 如何在PowerShell中获得本地主机名?
- 为什么在Java和。net中不能修改字符串?
- 'throw'和'throw new Exception()'的区别
- c# int到字节[]
- 如何跟踪log4net问题