2013-03-04 41 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