Search code examples
c#string-formattingcurrency-formatting

How to format a string as Vietnamese currency?


If I set Format in [Region and Language] to US...

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);

The result is: 12,345 (is correct)

But if I set the format in [Region and Language] to VietNam, then the result is: 12345

The result should be 12.345.

Can you help me? Thanks.


Solution

  • 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 "#,###"