localising currency (help me out)

It seems there are no utilities around to localise currency (correct me if I’m wrong, I’m writing in C), so I’m having to write my own. Regardless of the system localisation, I want to be able to refer to a currency (for which I have the currency code) in a localised way.

Thus, what I’m looking for is for your 3-letter currency code, how do you refer to positive and negative numbers? For example, in AUD it would be “$1.25″ and “−$1.25″.

About Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She doesn't really work on GNOME any more (sadly). Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.
This entry was posted in Uncategorized. Bookmark the permalink.

18 Responses to localising currency (help me out)

  1. Michael Gauthier says:

    Will libicu work for you?

  2. Danielle says:

    @Michael: doesn’t that just localise into the system localisation? I want to be able to format a currency based on that currency code.

  3. Not sure in C, but you can take a look at how Java does currency formatting: http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html and http://download.oracle.com/javase/6/docs/api/java/util/Currency.html

    TL;DR: For each currency it keeps two patterns separated by semicolon, the first for positive numbers, the second for negative numbers. Also it keeps track of the number of decimal places to be shown per currency.

  4. Danielle says:

    @Denis: this is similar to what I’ve done. I have entries for the positive and negative representations. The number of decimal places is being provided to be along with the amount and the currency code, so I don’t need that bit.

  5. Arve Eriksson says:

    Sweden: SEK
    Iceland: ISK
    Regarding positive/negative, it’s the same as for AUD.

  6. Michael Gauthier says:

    I think ICU will do what you want. You can specify the local and/or currency independently.

    See http://userguide.icu-project.org/formatparse/numbers#TOC-Currency-Formatting

  7. Elliott says:

    I think you should be able to figure it out from the stuff in http://ieng9.ucsd.edu/~cs30x/Std.C/locale.html using LC_MONETARY.

  8. Danielle says:

    @Arve: What about the symbol and the ordering. Is it “1.25 kr” and “-1.25kr”?

  9. Veeven says:

    Unicode Common Locale Data Repository (CLDR) has names and symbols and number formats (for ordering) of currencies in various locales. That list might not be full or complete in all locales yet, though.

  10. Hannes says:

    @Danielle: To be entirely correct it should be “1,25 kr” and “-1,25 kr” since Sweden (not sure about Iceland here) uses “,” and not “.” as decimal point. Still, I don’t think most people would notice which decimal point was used, as long as the two are not mixed.

    For example, writing 1,000 kr would mean 1 kr, and not 1000 kr. For large numbers it is instead common to divide by blanks: 1 200 450,00 kr is “one million, two hundred thousand, four hundred and fifty kronor”

    Currency localization is obviously not an easy subject :D

  11. Pieter says:

    Referring to Hannes’ comment, to add to the confusion, €1,000 means one thousand euro. As does €1000 and €1.000. Maybe not officially but it does when you ask a person in the street. On the other hand if it has two digits after the comma or point it is interpreted as cents. So €10,01 and €10.01 both mean ten euro and one cent to the average Euro-region person.

  12. Marcel says:

    I think the localization in the standard library (locale.h) already suffices, you can use nl_langinfo or localeconv to get the desired information. It is true that this will return values for the current locale but you can just: 1) save the current value for LC_MONETARY and LC_NUMERIC; 2) set it to the value you want; 3) call nl_langinfo or localeconv; 4) reset LC_MONETARY and LC_NUMERIC.
    I do not think you need any additional libraries.

  13. Bastien says:

    “locale -k LC_MONETARY”

    My guess is that things like:
    are what you want.

    int_p_cs_precedes 1 if the currency symbol precedes the internationally
    formatted monetary value for nonnegative values, 0 if
    it follows.

  14. Eetu Huisman says:

    Finland: EUR; 1 234,56 €; -1 234,56 €

  15. John says:

    In almost all cases formatting negative/positive representations of Money doesn’t depend on the Currency, it depends on the Locale of the person/system displaying the amount. If you want to display a list of multiple currencies, say in a currency converter, then you would format them all the same way for readability and to make it easier for the user to understand. Changing between . and , or symbol or sign position is just not good practise. You would need a very special use case for this to be otherwise.

    As you know, libc doesn’t know currency codes, only symbols. To get currency codes you will need libicu, or switch to KDE where KLocale / KCurrencyCode is fully aware of currency codes, multiple symbols, and the different formatting rules per locale :-) OK, KDE is not really an option, but if you’re looking for the data to write your own then ICU/CLDR or KDE will be a better source then crowd-sourcing. (Longer term, I’ll be switching KDE to an xml format for the currency data to make it easy to share).

  16. Johan Dahlin says:

    See strfmon(3) and localeconv(3)

  17. M Welinder says:

    Don’t trust locale.

    goffice/goffice/utils/formats.c has what you need, I think.

  18. Travis Reitter says:

    It’s probably just some weird accountant/tax form thing, but I’ve seen negative dollars represented as ($1.25). I only mention this because I can’t recall seeing formatting like -$1.25 anywhere — it’s generally written as a positive number and it’s expected the reader picks up the fact that it’s negative based on the context. I think I had a negative balance on a credit card when I accidentally overpaid it, and it used the -$1.25 representation, but I can’t remember for sure.

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>