0

I can't find the option of, given a number and a currency, printing them to a string given a culture. The closest thing I've found is decimal.ToString("C", GIVEN_CULTURE), but this won't ask for the currency, and will assume I'm talking about the currency the culture knows about. What if I work with USD in Europe?

Javascript, for instance, will request both parameters when building a string:

GIVEN_STRING.toLocaleString(culture, { style: 'currency', currency: currency })

And this is relevant, as the software I'm working on handles both USD and MXN (mexican peso). Our web dashboard displays different symbols regarding culture (Chrome examples, I think it varies cross-browser):

en:

  • USD has symbol "$"
  • MXN has symbol "MX$"

es-MX:

  • USD has symbol "US$"
  • MXN has symbol "$"
Roborowski
  • 11
  • 3
  • 1
    You have the currency information inside the Culture -> `CultureInfo.NumberFormat.Currency` – Nekeniehl Dec 13 '18 at 11:38
  • Yep, but that currency, in my case, being in Europe, is EURO. I want to display american Dollars, tho, I know the amount I want to print is USD. If I rely on the property you mentioned, it will print € instead! – Roborowski Dec 13 '18 at 11:44
  • The property CurrencySymbol has a setter, you can set it to whatever you want. – Nekeniehl Dec 13 '18 at 11:45
  • But then I would be using other aspects of the culture like CurrencyDecimalDigits or CurrencyDecimalSeparator "wrong", meaning that I would be displaying dollars as if they were Euros except for the symbol – Roborowski Dec 13 '18 at 11:49
  • When displaying a foreign currency, I would just use `dm.ToString("N2", localCulture)` and then append (or prepend) the ISO currency code, such as `"USD"` or `"MXN"`. I do not think many cultures have conventions on how to display other countries' currencies. – Jeppe Stig Nielsen Dec 13 '18 at 11:49
  • @JeppeStigNielsen that's what I was afraid of, but it seems like, at least Chrome, does have these conventions. I guess I would go for what Nekeniehl was proposing. Thx! – Roborowski Dec 13 '18 at 11:52
  • I don't see your point @Roborowski, you can specify whatever culture you want, `CultureInfo.GetCulture("en-GB")` or `CultureInfo.GetCulture("de-DE")` or whatever culture you need, and the you can set the CurrencySymbol to whatever you want if is not inside the selected culture, or call the ToString with different cultures. You can create your own culture too, I might not be understanding exactly what do you need. – Nekeniehl Dec 13 '18 at 11:52
  • Let's assume (I'm gonna make this up to make a point) that my culture displays currencies like this: €00,00, but I want to display different types of currencies, mine and a foreign one that I know should be displayed like this: 00.00$. With your solution, I would be displaying €00,00 and $00,00 (instead of 00.00$). But maybe that's perfectly valid, and I'm the one misinterpreting what culture defines. Does culture info define "Whenever you display MONEY do it like this" or "Whenever you display YOUR CURRENCY do it like this"? – Roborowski Dec 13 '18 at 12:04
  • I undestand now, please, check my answer. – Nekeniehl Dec 13 '18 at 12:06

2 Answers2

5

If all you worry about is the currency symbol, you can easily set it to whatever you like:

var format = (NumberFormatInfo)CultureInfo.CreateSpecificCulture("es-MX").NumberFormat.Clone();
format.CurrencySymbol = "US$";
decimal amount = 12.34m;
amount.ToString("C", format);
// OUTPUT:
// US$12.34

But usually there's more to formatting money values. As you can see, the value would be formatted very differently in Germany for example, because of specific cultural differences (like decimal point, currency symbol position, spacing, etc):

amount.ToString("C", CultureInfo.CreateSpecificCulture("de-DE"));
// OUTPUT:
// 12,34 €
marsze
  • 15,079
  • 5
  • 45
  • 61
0

I am not sure if I am understanding you question, but for example if you want a double to be expressed as a currency for different countries then you need to provide the different CultureInfo for it.

double foo = 3;
string fooGerman = foo.ToString("C", CultureInfo.GetCultureInfo("de-DE"));
string fooEnglish = foo.ToString("C", CultureInfo.GetCultureInfo("en-GB"));
Console.WriteLine(fooGerman);
Console.WriteLine(fooEnglish);

OUTPUT
3,00 €
£3.00

If that is not enough you can set the symbol for a given CultureInfo in CultureInfo.NumberFormat.CurrencySymbol and you can create your own IFormatProvider for the ToString() or create your own CultureInfo.

Nekeniehl
  • 1,633
  • 18
  • 35
  • My question can be reduced to "Does C# CultureInfo understand foreign currencies?". I guess the answer is no. Your answer solves my issue if the answer to the previous question is no. Thx! – Roborowski Dec 13 '18 at 12:14
  • 1
    CultureInfo DO NOT understand foreign currencies, but Javascript doesn't do either, you are specifying the currency regardless the Culture. Just think on how will be that implemented, for each culture, to know how others cultures express the currency. It is much efficient to just get the culture you need which contains how to express that currency. – Nekeniehl Dec 13 '18 at 12:25