C#:我有一个十进制数,我想用逗号分隔千位数字,但不对数字进行四舍五入。作为堆栈溢出的答案,我使用了 .ToString("#,##0")
但它对数字进行了四舍五入。对我来说四舍五入是不能接受的!
在我的模型中,我有:
公共(public)十进制? ADecimalNumber { 获取;放; }
在 Controller 中我有:
ViewBag.ADecimalNumber = 结果?.ADecimalNumber;
在 View 中我有:
@(ViewBag.ADecimalNumber != null ? ViewBag.ADecimalNumber.ToString("#,##0") : '-')
快速观看模式下,ViewBag 值为 11106.8314M
,ViewBag.ADecimalNumber.ToString("#,##0")
结果为 “11,107”
。
最佳答案
这是自定义说明符的默认行为。请参阅The "#" Custom Specifier .
The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.
关于c# - 为什么 ADecimalNumber.ToString ("#,##0") 对数字进行四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646129/