4

如果我将 [区域和语言] 中的格式设置为美国...

CultureInfo cul = CultureInfo.CurrentCulture;
string decimalSep = cul.NumberFormat.CurrencyDecimalSeparator;//decimalSep ='.'
string groupSep = cul.NumberFormat.CurrencyGroupSeparator;//groupSep=','
sFormat = string.Format("#{0}###", groupSep);
string a = double.Parse(12345).ToString(sFormat);

结果是:(12,345正确)

但是如果我将 [Region and Language] 中的格式设置为越南,那么结果是:12345

结果应该是12.345

你能帮助我吗?谢谢。

4

2 回答 2

8

You are helping too much. The format specifier is culture insensitive, you always use a comma to indicate where the grouping character goes. Which is then substituted by the actual grouping character when the string is formatted.

This formats correctly:

        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        string a = double.Parse("12345").ToString("#,###", cul.NumberFormat);

You should actually use "#,#" to ensure it still works in cultures that have a uncommon grouping. It wasn't clear from the question whether that mattered or not so I punted for "#,###"

于 2012-09-01T11:35:51.747 回答
3
于 2012-09-01T06:44:42.623 回答