Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 451
David Avatar asked Dec 21 '22 16:12

David


2 Answers

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

like image 111
Hans Passant Avatar answered Dec 30 '22 09:12

Hans Passant


Try something like this:

var value = 8012.34m;
var info = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
Console.WriteLine(String.Format(info, "{0:c}", value));

The result is:

8.012,34 ₫

Oh, and with the value 12345 the result is 12.345,00 ₫.

like image 37
Enigmativity Avatar answered Dec 30 '22 11:12

Enigmativity