用C#格式的十进制格式,至少2个小数位 [英] Format decimal in C# with at least 2 decimal places

查看:71
本文介绍了用C#格式的十进制格式,至少2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个显示格式化程序,该格式化程序将在不进行四舍五入的情况下输出小数作为C#中的这些字符串表示形式?

Is there a display formatter that will output decimals as these string representations in C# without doing any rounding?

小数可能有2个小数位,但是如果有

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.

例如:

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

预先感谢

推荐答案

当您有合理的最大数字位数上限时:

When you have a reasonable upper limit for the maximum number of digits:

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

将处理您的所有示例,但不处理 1.23456789012345M

will handle all your examples but not 1.23456789012345M

这篇关于用C#格式的十进制格式,至少2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆