c# - 为什么 ADecimalNumber.ToString ("#,##0") 对数字进行四舍五入?

标签 c# string

C#:我有一个十进制数,我想用逗号分隔千位数字,但不对数字进行四舍五入。作为堆栈溢出的答案,我使用了 .ToString("#,##0") 但它对数字进行了四舍五入。对我来说四舍五入是不能接受的!

在我的模型中,我有:
公共(public)十进制? ADecimalNumber { 获取;放; }
在 Controller 中我有:
ViewBag.ADecimalNumber = 结果?.ADecimalNumber;
在 View 中我有:
@(ViewBag.ADecimalNumber != null ? ViewBag.ADecimalNumber.ToString("#,##0") : '-')

快速观看模式下,ViewBag 值为 11106.8314MViewBag.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/

相关文章:

c# - 为什么某些证书的 Request.ClientCertificate 为空,而其他证书则为空?

c# - 为什么 aforge.net 无法检测所有网络摄像头分辨率

c++ - 从函数返回局部字符数组

c# - 如何将嵌入式 list 移动到外部 .manifest 文件?

c# - Xamarin C# 有趣的代码标题

R - 匹配两列之间的字符串

c++ - 在运行时将 char 数组转换为 String?

c - C 中的这个程序(反转字符串)不起作用是什么?

c++ - 我怎样才能在一个变量中存储多个字母?

c# - 如何保护图书馆的用户免受错误的初始化?