Ios NSNumberFormatter负值上的显示括号
我希望格式化程序将-4.00英镑显示为-4.00英镑,但它一直将其显示为($4.00),这是为什么 下面的函数将字符串“00000014”转换为“£00.14”,它也应转换为负值。但是数字格式化程序似乎添加了括号 代码如下:Ios NSNumberFormatter负值上的显示括号,ios,objective-c,nsnumberformatter,Ios,Objective C,Nsnumberformatter,我希望格式化程序将-4.00英镑显示为-4.00英镑,但它一直将其显示为($4.00),这是为什么 下面的函数将字符串“00000014”转换为“£00.14”,它也应转换为负值。但是数字格式化程序似乎添加了括号 代码如下: +(NSString *)pfsCurrencyAmountString:(NSString *)amount CurrencyCodeAsString:(NSString *)code{ if (amount == nil) { return nil; }
+(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];
}
这似乎很奇怪。当你使用
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
它打印在括号中,但如果您使用其他国家的其他标识符,则打印正确
我可以看到两种方式:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"];
NSNumberFormatterCurrencyStyle
货币值可以为负值:O???请在询问新答案之前搜索现有答案。顺便说一句,[currencyStyle setNumberStyle:@“NSNumberFormatterCurrencyStyle”]代码>行不正确;使用[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]代码>正如您稍后所做的。这是因为您需要考虑区域设置以及数字、日期等。不幸的是,cocoa在每个区域设置中所做的事情并没有很好的文档记录。有些是重症监护病房的,有些是苹果烤的。我建议用苹果填补一个文档漏洞。嗯。。。它与iOS8上的相同