[英]Math rounding in c# - whole numbers issue
<%= Math.Round(keyRate, 5) %>
So in our view, we use this to display a bunch of numbers. 所以在我们看来,我们使用它来显示一堆数字。 it seems when we have a whole number, it rounds to 2 decimals instead of 5. Is this expected behavior?
看来,当我们有一个整数时,它会舍入到2位小数而不是5位。这是预期的行为吗? If so, how would I change it.
如果是这样,我将如何更改它。
Thanks! 谢谢!
There's a difference between what a number is and how it's displayed when written out. 数字是什么以及在写出时它的显示方式之间存在差异。 You may need to override ToString() with ToString(string)
您可能需要使用ToString(字符串)覆盖ToString( )
Per the Custom Numeric Formats , if you want it to be 5 places, you would use 根据自定义数字格式 ,如果您希望它是5个位置,您可以使用
Math.Round(keyRate, 5).ToString(00.00000)
More information on the "0" custom specifier . 有关“0”自定义说明符的更多信息 。
The second parameter to Math.Round(d, decimals)
is the number of fractional digits that will be returned at most . 到第二参数
Math.Round(d, decimals)
是将至多被返回小数的位数。 From MSDN : 来自MSDN :
If the precision of d is less than decimals, d is returned unchanged .
如果d的精度小于小数,则返回d 不变 。
In your case you are passing a number with less fractional digits, so it remains unchanged. 在您的情况下,您传递的是一个数字较小的数字,因此它保持不变。
How do you know that it is rounding to 2 decimals while you are passing a whole number? 当你传递一个整数时,你怎么知道它是四舍五入到小数点? My guess is that you are outputting only 2 decimals.
我的猜测是你只输出2位小数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.