2

I would normally just use this:

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

which works great. I had an "overwrite" for canadian dollars to make sure that people would see the difference between US and Canadian dollars:

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

But now people are asking output like:

CAD 1234,56

So I used:

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

which works, but I am wondering if this is the best approach to get the 3 char iso currency symbol in front of the float/double. So instead of using the CultureInfo I have to use RegionInfo now?

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
  • Seems like there is no "easy" way to do it. Starting from http://stackoverflow.com/a/22837610/1336590 you might want to have `cultureInfo` as input parameter of the method. Then I'd have `string currencyValue = value.ToString("C", cultureInfo);` to get the "correct" currency string. Then remove the currency symbol, trim and prepend the `ISOCurrencySymbol` of the `regionInfo`. --- Btw. use `decimal` instead of `double` for monetary values to avoid headaches. – Corak May 23 '16 at 10:22
  • This does solve the thousand separator, thanks for this suggestion! – JP Hellemons May 23 '16 at 10:26
  • So basically: `public static string FormatCurrency(decimal value, CultureInfo cultureInfo) { return string.Concat(new RegionInfo(cultureInfo.LCID).ISOCurrencySymbol, " ", value.ToString("C", cultureInfo).Replace(cultureInfo.NumberFormat.CurrencySymbol, string.Empty).Trim()); }` --- and tested with `FormatCurrency(1234.5678m, CultureInfo.GetCultureInfo("en-CA"))` and `FormatCurrency(1234.5678m, CultureInfo.GetCultureInfo("de-DE"))` – Corak May 23 '16 at 10:35
  • Please post as answer, so that I can give you credits and mark it as accepted. – JP Hellemons May 23 '16 at 10:58

2 Answers2

3

The correct way would be:

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

If you set CurrencyPositivePattern = 2 you make sure, the symbol is placed in front of the number with an additional empty space (see also documentation for CurrencyPositivePattern), CurrencyNegativePattern = 12 does the same for negative values (documentation for CurrencyNegativePattern).

output:

CAD 1,234.57

and for negative

CAD -1,234.57

abto
  • 1,583
  • 1
  • 12
  • 31
0

as @Corak explains:

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

output:

CAD 1,234.57

@Coraks suggestion added:

Console.WriteLine(ca.ISOCurrencySymbol + " " + test.ToString("C", cai).Replace(cai.CurrencySymbol, ""));
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
  • 1
    This will fail for cultures where the currency symbol is not (directly) in front of the value. Try it with `"de-DE"` for example. – Corak May 23 '16 at 10:41
  • True, would have to do `.Replace(cai.CurrencySymbol, "")` instead of `.Substring(1)`. Thanks for this good suggestion! – JP Hellemons May 23 '16 at 10:57