我希望格式化程序顯示 - 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];
}
'NSNumberFormatterCurrencyStyle'貨幣值可以是負數:O ??? –
請在尋求新的答案之前尋找現有的答案。 –
順便說一句,'[currencyStyle setNumberStyle:@「NSNumberFormatterCurrencyStyle」];'行是不正確的;使用'[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];'就像你以後做的那樣。 – Rob