【问题标题】:NSNumberFormatter display brackets on negative values [duplicate]NSNumberFormatter 在负值上显示括号[重复]
【发布时间】:2013-08-05 12:07:06
【问题描述】:

我希望格式化程序将 -£4.00 显示为 -£4.00,但它一直显示为 (£4.00),这是为什么呢?

下面的这个函数将字符串“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];,就像你稍后所做的那样。

标签: ios objective-c nsnumberformatter


【解决方案1】:

这看起来很奇怪。使用时,

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

它打印在括号中,但如果您使用其他国家的其他标识符,它打印正确。

我可以看到两种方式:

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

    [currencyStyle setFormat:@"¤#,##0.00"];

2.另一个不太合适的方法是使用澳大利亚的标识符。它的格式与您需要的相同。

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

【讨论】:

  • 这是因为您需要将语言环境与数字、日期等因素考虑在内。不幸的是,cocoa 在每个语言环境中所做的事情并没有很好的记录。有些来自 ICU,有些来自 Apple。我建议向 Apple 填写文档错误。
  • 嗯......它与iOS8上的pl_PL相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多