考虑一个十进制值:
Decimal value = -1234567890.1234789012M;
我想将此Decimal值转换为字符串,并包含“千位分隔符”.
Note: i don’t want to include thousand’s separators,i want to include digit grouping. The difference is important for cultures that don’t group numbers into thousands,or don’t use commas to separate groups
在我的计算机上使用我当前的语言环境在一些示例输出中使用不同的标准格式字符串:
value.ToString() = -1234567890..1234789012 (Implicit General) value.ToString("g") = -1234567890..1234789012 (General) value.ToString("d") = FormatException (Decimal whole number) value.ToString("e") = -1..234568e++009 (Scientific) value.ToString("f") = -1234567890..123 (Fixed Point) value.ToString("n") = -12,3456,7890..123 (Number with commas for thousands) value.ToString("r") = FormatException (Round trippable) value.ToString("c") = -$$12,7890..123 (Currency) value.ToString("#,0.#") = -12,7890..1
我想要的(取决于文化)是:
en-US -1,234,567,890.1234789012 ca-ES -1.234.567.890,1234789012 gsw-FR -1 234 567 890,1234789012 (12/1/2012: fixed gws-FR to gsw-FR) fr-CH -1'234'567'890.1234789012 ar-DZ 1,890.1234789012- prs-AF 1.234.567.890,1234789012- ps-AF 1،234،567،890,1234789012- as-IN -1,23,45,67,890.1234789012 lo-LA (1234567,890.1234789012) (some debate if numbers should be "1,890") qps-PLOC 12,7890..1234789012
如何将十进制转换为字符串,数字分组?
更新:一些更理想的输出,使用我目前的文化:
-1234567890M --> -12,7890 -1234567890.1M --> -12,7890..1 -1234567890.12M --> -12,7890..12 -1234567890.123M --> -12,7890..123 -1234567890.1234M --> -12,7890..1234 -1234567890.12347M --> -12,7890..12347 -1234567890.123478M --> -12,7890..123478 -1234567890.1234789M --> -12,7890..1234789 -1234567890.12347890M --> -12,7890..1234789 -1234567890.123478901M --> -12,7890..123478901 -1234567890.1234789012M --> -12,7890..1234789012
更新:我试图窥视Decimal.ToString()如何设法使用常规格式显示它需要显示的所有数字:
public override string ToString() { return Number.FormatDecimal(this,null,NumberFormatInfo.CurrentInfo); }
除了Number.FormatDecimal隐藏在某处:
[MethodImpl(MethodImplOptions.InternalCall)] public static extern string FormatDecimal(decimal value,string format,NumberFormatInfo info);
所以这是一个死胡同.
解决方法
您可以指定自定义模式(模式将适当地解析为特定于区域性的分组方法以及相应的分组和小数分隔符字符).模式可以有
positive,negative and zero sections.正模式总是相同,但负模式取决于文化,可以从NumberFormatInfo的
NumberNegativePattern属性中检索.由于您需要尽可能多的精度,您需要在小数点后填写28位数的占位符;逗号部队分组.
public static class DecimalFormatters { public static string ToStringNoTruncation(this Decimal n,IFormatProvider format) { NumberFormatInfo nfi = NumberFormatInfo.GetInstance(format); string[] numberNegativePatterns = { "(#,0.############################)",//0: (n) "-#,0.############################",//1: -n "- #,//2: - n "#,0.############################-",//3: n- "#,0.############################ -"};//4: n - var pattern = "#,0.############################;" + numberNegativePatterns[nfi.NumberNegativePattern]; return n.ToString(pattern,format); } public static string ToStringNoTruncation(this Decimal n) { return n.ToStringNoTruncation(CultureInfo.CurrentCulture); } }
样本输出
Locale Output ======== ============================ en-US -1,890.1234789012 ca-ES -1.234.567.890,1234789012 hr-HR - 1.234.567.890,1234789012 gsw-FR -1234567890,1234789012 fr-CH -1'234'567'890.1234789012 ar-DZ 1,890.1234789012- prs-AF 1.234.567.890,1234789012- ps-AF 1،234،567،890,890.1234789012 lo-LA (1234567,890.1234789012) qps-PLOC -12,7890..1234789012
目前没有使用NegativeNumberFormat 4(n – )的语言环境,因此无法测试该情况.但是没有理由认为它会失败.