简体   繁体   English

在C#中格式化小数,至少有2个小数位

[英]Format decimal in C# with at least 2 decimal places

Is there a display formatter that will output decimals as these string representations in C# without doing any rounding? 是否有一个显示格式化程序,它将在C#中输出小数作为这些字符串表示而不进行任何舍入?

The decimal may have 2 decimal places, but if it has more precision it should be included in the resultant string and not rounded off. 小数可能有2个小数位,但如果它具有更高的精度,它应该包含在结果字符串中而不是四舍五入。

Examples: 例子:

decimal  -> string
20       -> 20,00
20.00    -> 20,00
20.5     -> 20,50
20.5000  -> 20,50
20.125   -> 20,125
20.12500 -> 20,125

Thanks in advance 提前致谢

When you have a reasonable upper limit for the maximum number of digits: 如果您有最大位数的合理上限:

 ToString("0.00#######")

will handle all your examples but not 1.23456789012345M 将处理您的所有示例,但不处理 1.23456789012345M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM