realbart realbart - 11 months ago 80
C# Question

How to use CultureInfo to format deprecated currencies?

In dotnet, the recommended way of formatting currencies in a culture-specific way, is (as far as I can find):

using System.Globalization
var info = CultureInfo.GetCultureInfo("en-GB")
return string.Format(info, "{0:c}", 1234.5678)

This returns:


However. No specific currency is given here. So if Great Britain ever converts to Euro, the same method would suddenly return something like:

€ 1,234.57

This has just happened to Latvia. This year, it converted to the euro. Various systems contain both amounts in letvian lats and euros. I need to be able to still print and format old currencies. And I need to be sure new currencies can also be handled. This formatting is not just about the symbol, but also about the placement (before, after, with or without space) and the local number format (decimal separator and thousands separator).

Every amount in our database has a currency (e.g. EUR) and a corresponding culture (e.g. nl-NL). Euro amounts are formatting differently depending on if they originate from our German or our Dutch office. (they both use euros, but format the amounts differently)

  • Does dotnet provide access to older currencies?

  • What is a future-proof way of writing the formatting amounts?

Right now, the only way I can think of, is to copy the culture-database to my database.

Answer Source

You can create a custom CultureInfo (by cloning one and modifying) which uses a different currency symbol/format (ie. set its NumberFormat to a different instance of NumberFormatInfo).

Then pass this CultureInfo to formatting functions as needed.

As the comment to the question notes, .NET (and Windows in general) doesn't provide historic data (similarly for time zones but there's a library for that). In the cases you need you'll need to hold enough data yourself.

Remember ISO-4217 currency codes are not reused under such a change, so holding that against amounts allows you to format correctly. Additionally just because a country formats their currency amounts one way doesn't mean everyone does. Eg. 25 French Francs was be "FF25.00" in the UK but "25FF00" or "FF25,00" in other locales. (EDIT: I note you covered this last paragraph in the question.)