2013-08-05 77 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