问题描述
我想将 String.Format
设置为小数,以便它既具有千位分隔符又具有强制的小数位(3).
I'd like to String.Format
a decimal so that it has both thousand separators and forced decimal places (3).
例如:
输入:
123456,12
78545,8
输出:
123.456,120
78.545,800
我尝试过
String.Format("{0:0.0,000}", input);
但这只能给出千位分隔符,而不能强制保留小数位.
but this only gives the thousand separators but doesn't force the decimal places.
推荐答案
在 自定义数字中格式 字符串,句点(.
)用于本地化的十进制分隔符".IE.即使您当前的语言环境使用逗号作为小数点分隔符,也应在格式字符串中使用句点.类似地,逗号(,
)用于本地化的千位分隔符.
In a custom numeric format string a period (.
) is used for a "localized decimal separator". Ie. even if your current locale uses a comma as a decimal separator you should use a period in a format string. Similarly comma (,
) is used for the localised thousands separator.
当您的格式将逗号放在之后时,事情将变得混乱(小数点后不使用千位分隔符).
As your format puts the comma after the period things are going to get confused (thousands separators don't apply after the decimal point).
所以尝试:
String.Format("{0:#,##0.000}", input);
(仅当 input
足够大时才使用#
来包含数字.)
(Using #
for digits to only include if input
is large enough.)
这篇关于具有十进制分隔符和强制小数位的String.Format十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!