rein
rein

Reputation: 33455

Determining the number of decimal numbers for a currency

On the iPhone:

Using the US locale, a currency looks like this: $1,234.56
Using the UK locale, a currency looks like this: £1,234.56
Using the German (Germany) locale, a currency looks like this: 1.234,56 €

and finally, a Japanese currency: ¥1,234

The Japanese currency has no decimals and this impacts my custom keyboard significantly. I'm trying to find a method in the Cocoa-touch framework which will tell me how many decimal places a specific currency has - my hard-coded value of 2 isn't doing me justice :(

Can anyone help?

Upvotes: 5

Views: 1335

Answers (4)

Joe Regan
Joe Regan

Reputation: 498

I had a similar problem but the answers above didn't really solve my problem.

I ended up using the maximumFractionDigits method on NSNumberFormatter which gave me 0 for Japanese locale. Make sure you use a NSNumberFormatterCurrencyStyle for the formatter, otherwise you'll see decimal places in other formatters.

    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setLocale:[NSLocale currentLocale]];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    NSLog(@"Number of decimal places %i", [currencyFormatter maximumFractionDigits]);

Upvotes: 0

RichardCase
RichardCase

Reputation: 431

You should be able to use the CFNumberFormatter objects to get the information you need. Specifically you could use CFNumberFormatterGetDecimalInfoForCurrencyCode:

CFStringRef localeIdent = CFSTR("JPY");

int numDecimals;
double rounding;
BOOL result = CFNumberFormatterGetDecimalInfoForCurrencyCode(localeIdent, &numDecimals, &rounding);

I hope that helps.

Upvotes: 3

Steve Mc
Steve Mc

Reputation: 211

I haven't programmed in Cocoa for ages, but from the documentation for NSNumberFormatter, there's a function called 'currencyDecimalSeparator' - that might at least tell you if a currency has one at all, which might be a start?

Upvotes: 2

Nat
Nat

Reputation: 9951

Financial companies maintain databases of this kind of information. You might be able to buy the data or import it from an online source.

Note also: some currencies need three or four decimal places. See http://www.londonfx.co.uk/ccylist.html for examples.

Upvotes: 0

Related Questions