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”.
Will libicu work for you?
@Michael: doesn’t that just localise into the system localisation? I want to be able to format a currency based on that currency code.
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.
@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.
Sweden: SEK
Iceland: ISK
Regarding positive/negative, it’s the same as for AUD.
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
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.
@Arve: What about the symbol and the ordering. Is it “1.25 kr” and “-1.25kr”?
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.
@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 😀
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.
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.
“locale -k LC_MONETARY”
My guess is that things like:
int_p_sign_posn=1
int_n_sign_posn=1
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.
Finland: EUR; 1 234,56 €; -1 234,56 €
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).
See strfmon(3) and localeconv(3)
Don’t trust locale.
goffice/goffice/utils/formats.c has what you need, I think.
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.