5

Many languages, such as .NET languages and Java come with a currency formatting facility built-in. What they do is format a number using a culture-specific number format and add the culture's currency symbol.

The problem here is that the number format is strongly coupled to the currency symbol. In practice the number format should be the correct one for the language of the surrounding text, while the currency symbol should be for the currency being talked about. For example, you wouldn't use American thousand separators when talking about US Dollars in a document that's in German.

Can you think of a practical use for this kind of currency formatting functions, or do they exist just to impress money-oriented management people?

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • Is the symbol for the currency unit always tied to the separator symbol, or are they separable? PHP's `money_format` function allows pretty tight control over the formatted string, including the ability to suppress the currency symbol. – Dagg Nabbit Jul 27 '10 at 22:26
  • Is it not useful for an global application used by users in different countries who want money displayed in their local format? Or am I missing the point? – Chris Diver Jul 27 '10 at 22:28
  • @no: .NET seems to allow you to change the currency symbol (takes a few lines of code though, which is a step away from clean format string. In some contexts you're only allowed to specify a format string, too). I couldn't find a way to do it in Java, but I'm not exactly a Java expert so I can't say for sure. – Matti Virkkunen Jul 27 '10 at 22:36
  • @Chris: Yes, you are missing the point. I'm talking about the (rather common) case where the number format and currency symbol should come from different cultures. – Matti Virkkunen Jul 27 '10 at 22:37

1 Answers1

1

At least in .NET the number format is tied to a culture, not the symbol. The default currency symbol is the primary one associated with that culture. The symbol can be overridden as required.

Like your example, if I'm embedding a currency value in a string then I use the string's culture for the format, but override the currency symbol appropriately. The rest of the formatting is left as-is.

On the other hand if I'm doing a report all of the values are formatted using the same culture. I normally use the ISO 3-character codes instead of currency symbols in this case, especially if more than 2 currencies are involved, to avoid font issues.

Ryan's answer to another question describes the what and how Best Practices for currency formatting in C#.

Community
  • 1
  • 1
devstuff
  • 8,277
  • 1
  • 27
  • 33
  • Changing the currency symbol is a good point, although it requires an awful lot of code for something that IMHO should be a part of the format parameters. It's also easy to run into problems with currency symbols displayed on the wrong size ($50 vs. 50 kr), not to mention that some currencies actually change order depending on the locale (€50 vs. 50€) – Matti Virkkunen Jul 27 '10 at 22:46
  • 1
    I ended up writing an extension method on `decimal` to handle all the drudge work, somewhat similar to Jon Skeet's example here: http://stackoverflow.com/questions/1071273/currency-formatting/1071302#1071302 – devstuff Jul 28 '10 at 03:22