float number = 3.4567070Math.Round(number,2)Math.Truncate(100 * number) / 100
哪个是最好的,最有效的?何时使用圆形和截断?
解决方法 截断效率略高,但这些功能非常不同.> Math.Round
计算输入的最近数字到指定的准确度.
Rounds a value to the nearest integer or to the specifIEd number of fractional digits.
> Math.Truncate
有效地丢弃小数点后的任何数字.它将始终向零舍入到最接近的整数.
Calculates the integral part of a number
您应该使用哪种功能完全取决于您尝试实现的逻辑.
这是每个函数的快速示例输出:
input | Round | Truncate------+-------+--------- -1.6 | -2 | -1 -1.5 | -2 | -1 -1.4 | -1 | -1 -0.5 | 0 | 0 0.5 | 0 | 0 1.4 | 1 | 1 1.5 | 2 | 1 1.6 | 2 | 1总结
以上是内存溢出为你收集整理的C#舍入2位小数:截断与舍入全部内容,希望文章能够帮你解决C#舍入2位小数:截断与舍入所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)