How to format a string as Vietnamese currency?

六月ゝ 毕业季﹏ 提交于 2019-12-06 07:33:51

问题


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.


回答1:


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




回答2:


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 ₫.



来源:https://stackoverflow.com/questions/12225182/how-to-format-a-string-as-vietnamese-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!