货币格式C#ISOCurrencySymbol

时间:2016-05-23 09:58:43

标签: c# currency cultureinfo regioninfo

我通常会使用它:

double test = 1234.5678;
Console.WriteLine(test.ToString("C"));

效果很好。我有一个"覆盖"为加拿大元确保人们会看到美元和加元之间的差异:

var canadaFi = (NumberFormatInfo)CultureInfo.GetCultureInfo("en-CA").NumberFormat.Clone();
canadaFi.CurrencySymbol = "C$ ";
Console.WriteLine(val.ToString("C", canadaFi));

但是现在人们都在问输出:

  

CAD 1234,56

所以我用过:

RegionInfo ca = new RegionInfo("en-CA");
Console.WriteLine(string.Format("{0} {1}", ca.ISOCurrencySymbol, test.ToString("f2")));

哪个有效,但我想知道如果这是最好的方法在float / double前面获得3个char iso货币符号。 那么我现在不必使用CultureInfo而是使用RegionInfo吗?

2 个答案:

答案 0 :(得分:2)

正确的方法是:

const string canada = "en-CA";

var ca = new RegionInfo(canada);
var cai = new CultureInfo(canada)
{
    NumberFormat =
    {
        CurrencySymbol = ca.ISOCurrencySymbol,
        CurrencyPositivePattern = 2,
        CurrencyNegativePattern = 12
    }
};

Console.WriteLine(test.ToString("C", cai));

如果设置CurrencyPositivePattern = 2,请确保该符号位于数字前面,并带有额外的空格(另请参阅documentation for CurrencyPositivePattern),CurrencyNegativePattern = 12对负值执行相同操作(documentation for CurrencyNegativePattern)。

输出:

  

CAD 1,234.57

和负面

  

CAD -1,234.57

答案 1 :(得分:0)

正如@Corak所解释的那样:

CultureInfo cai = new CultureInfo("en-CA");
RegionInfo ca = new RegionInfo("en-CA");
Console.WriteLine(ca.ISOCurrencySymbol + " " + test.ToString("C", cai).Substring(1));

输出:

  

CAD 1,234.57

@Coraks建议补充道:

Console.WriteLine(ca.ISOCurrencySymbol + " " + test.ToString("C", cai).Replace(cai.CurrencySymbol, ""));