如何将字符串格式化为越南货币? [英] 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);
结果是:12,345
(正确)
但是,如果我将[区域和语言]中的格式设置为VietNam,则结果为:12345
But if I set the format in [Region and Language] to VietNam, then the result is: 12345
结果应为12.345
.
你能帮我吗?谢谢.
推荐答案
您正在提供过多帮助.格式说明符不区分区域性,您始终使用逗号指示分组字符的位置.格式化字符串后,将使用实际的分组字符替换该字符.
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.
此格式正确:
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 "#,###"
这篇关于如何将字符串格式化为越南货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!