2013-03-04 165 views
2
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; 
[numberFormatter setLocale:product.priceLocale]; 
NSString *localizedMoneyString = [numberFormatter stringFromNumber:product.price]; 
[numberFormatter release]; 

结果:€0.89 预期结果:0,89€NSNumberFormatter错误的结果

无论setFormatterBehavior值的结果是一样的。

回答

4

是什么让你觉得这个结果是错误的?

货币指标的位置和符号不仅仅取决于货币,它取决于区域设置。

我举一个例子,让我们有货币代码EUR(欧元)。 现在尝试格式化一个区域设置为en_US(英语,美国)和cs_CZ(捷克,捷克共和国)的金额。您将分别获得€0.890,89€

即使符号可以改变,让我们尝试将其与货币代码CZK(捷克克朗) - 分别,你会得到 CZK 0.890,89 Kc

鉴于语言环境和货币代码,您的结果可能绝对正确。

编辑:

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

输出:

BGN
.

正如你可以看到,现场是正确的初始化,与区域设置为保加利亚(当地货币代码是BGN)和语言设置为英文(小数点分隔符是一个点)。请注意,数字格式不是从该区域设置的。它由语言设置。

所以,问题不是NSNumberFormatter的结果,问题在于你的期望,这是错误的。

+0

使用区域设置 - 'en_BG @ currency = EUR',预期结果是'0.89€' – h4cky 2013-03-04 12:14:59

+0

但是为什么货币符号不是在价格之后? – h4cky 2013-03-04 12:17:32

+0

使用此语言环境查看应用商店中的价格标签 - 价格格式必须相同。 – pcholberg 2013-03-04 12:18:37