Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

 £1,234.57

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.

like image 742
realbart Avatar asked Nov 02 '22 07:11

realbart


2 Answers

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

like image 147
Richard Avatar answered Nov 09 '22 10:11

Richard


Of course, the simplest way is to not use the locale-specific currency format, but rather to format the amount as a simple number and pre- or suffix it with the ISO currency code. The convention

       ------------
Sum:   ATS 1.376,00  (= EUR 100,00)

is commonly found on invoices (using locale de-AT as an example).

If you want to use the built-in currency formatting options, I would suggest to replace the currency symbol with the one stored in the database. I.e., in your currency table, you'd need to map currencies to symbols:

EUR -> €
ATS -> S
...

and then you'd replace myCultureInfo.NumberFormat.CurrencySymbol with the one in the database. That way, you ensure that a value is never shown with the wrong currency symbol.

like image 41
Heinzi Avatar answered Nov 09 '22 10:11

Heinzi