h4cky
h4cky

Reputation: 894

NSNumberFormatter wrong result

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *localizedMoneyString = [numberFormatter stringFromNumber:product.price];
[numberFormatter release];

Result: €0.89 Expected result: 0,89€

No matter setFormatterBehavior value the result is the same.

Upvotes: 2

Views: 619

Answers (1)

Sulthan
Sulthan

Reputation: 130072

What makes you think this result is wrong?

The position and symbol of the currency indicator doesn't depend only on the currency, it depends on the locale.

I'll give an example, let's have currency code EUR (Euro). Now try to format an amount with a locale set to en_US (English, USA) and with cs_CZ (Czech, Czech Republic). You'll get €0.89 and 0,89€ respectively.

Even the symbol can change, let's try it with currency code CZK (Czech koruna) - you'll get CZK 0.89 and 0,89 Kc respectively.

Your result is probably absolutely correct given the locale and currency code.

EDIT:

NSLocale* locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_BG"] autorelease];
NSLog(@"%@", [locale objectForKey:NSLocaleCurrencyCode]);
NSLog(@"%@", [locale objectForKey:NSLocaleDecimalSeparator]);

Outputs:

BGN
.

As you can see, the locale is initialized correctly, with region set to Bulgaria (local currency code is BGN) and language set to English (decimal separator is a point). Note that the number formatting is not set from the region. It is set by the language.

So, the problem is not the NSNumberFormatter result, the problem is in your expectation, which is wrong.

Upvotes: 4

Related Questions