2013-08-05 43 views
4

我希望格式化程序顯示 - 4.00英鎊爲 - 4.00英鎊,但它一直顯示爲(4.00英鎊),爲什麼?NSNumberFormatter負值顯示括號

下面的函數將字符串「00000014」變成「£00.14」,它也應該變成負值。但數字格式化程序似乎添加了括號。

代碼如下:

+(NSString *)pfsCurrencyAmountString:(NSString *)amount CurrencyCodeAsString:(NSString *)code{ 

if (amount == nil) { 
    return nil; 
} 

int amountLength = [amount length]; 
NSMutableString *amountMutable = [NSMutableString stringWithString:amount]; 

if (amountLength > 2) { 
    [amountMutable insertString:@"." atIndex:amountLength - 2]; 
} 

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 

NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[currencyStyle setNumberStyle:@"NSNumberFormatterCurrencyStyle"]; 
[currencyStyle setLocale:locale]; 
[currencyStyle setCurrencyCode:code]; 

NSNumber * balance = [currencyStyle numberFromString:amountMutable]; 
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

return [currencyStyle stringFromNumber:balance]; 
} 
+0

'NSNumberFormatterCurrencyStyle'貨幣值可以是負數:O ??? –

+0

請在尋求新的答案之前尋找現有的答案。 –

+0

順便說一句,'[currencyStyle setNumberStyle:@「NSNumberFormatterCurrencyStyle」];'行是不正確的;使用'[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];'就像你以後做的那樣。 – Rob

回答

2

這似乎不可思議。當您使用,

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 

它打印在括號中,但如果您使用其他國家的其他標識符,它打印正確。

我可以看到兩種方式:

  1. 創建自己的格式化程序:

    [currencyStyle setFormat:@ 「¤#,## 0.00」];

2.另一種不正確的方式是使用澳大利亞的標識符。它的格式與你需要的相同。

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"]; 
+0

這是因爲你需要考慮數字,日期等語言環境,不幸的是,每個地區的可可豆精確地記錄下來並沒有很好的記錄。有些來自ICU,有些來自Apple。我建議用Apple填寫文檔錯誤。 – uchuugaka

+0

好吧...它和iOS8上的pl_PL一樣 – Marcin