带有NSNumberFormatterCurrencyStyle的NSNumberFormatter:负值的numberFromString似乎关闭了一个

时间:2012-07-16 14:35:57

标签: objective-c ios nsnumberformatter

我遇到了NSNumberFormatter上带有NSNumberFormatterCurrencyStyle和负最小值的numberFromString的明显的一对一问题。从下面的测试中可以看出,numberFromString在尝试转换最小量时返回nil,但在最小值下返回正确。

我是否误解了“最小”在负面背景下的含义?或者这是numberFromString中的错误?还是?

感谢。

- (void)test {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setMinimum:[NSDecimalNumber decimalNumberWithMantissa:2142 exponent:-2 isNegative:YES]];
    [numberFormatter setMaximum:[NSDecimalNumber decimalNumberWithMantissa:100 exponent:0 isNegative:NO]];
    [numberFormatter setNegativeFormat:[@"-" stringByAppendingString:[numberFormatter positiveFormat]]];

    NSLog(@"[numberFormatter positiveFormat] is: %@", [numberFormatter positiveFormat]);
    NSLog(@"[numberFormatter negativeFormat] is: %@", [numberFormatter negativeFormat]);
    NSLog(@"[numberFormatter minimum] is: %@", [numberFormatter minimum]);
    NSLog(@"[numberFormatter maximum] is: %@", [numberFormatter maximum]);
    NSLog(@"[numberFormatter numberFromString:@\"-$21.43\"] is: %@", [numberFormatter numberFromString:@"-$21.43"]);
    NSLog(@"[numberFormatter numberFromString:@\"-$21.42\"] is: %@", [numberFormatter numberFromString:@"-$21.42"]);
    NSLog(@"[numberFormatter numberFromString:@\"-$21.41\"] is: %@", [numberFormatter numberFromString:@"-$21.41"]);
    NSLog(@"[numberFormatter numberFromString:@\"-$21.40\"] is: %@", [numberFormatter numberFromString:@"-$21.40"]);

}

控制台输出:

2012-07-16 10:13:04.242 Am[21075:15203] [numberFormatter positiveFormat] is: ¤#,##0.00
2012-07-16 10:13:04.244 Am[21075:15203] [numberFormatter negativeFormat] is: -¤#,##0.00
2012-07-16 10:13:04.244 Am[21075:15203] [numberFormatter minimum] is: -21.42
2012-07-16 10:13:04.245 Am[21075:15203] [numberFormatter maximum] is: 100
2012-07-16 10:13:04.246 Am[21135:15203] [numberFormatter numberFromString:@"-$21.43"] is: (null)
2012-07-16 10:13:04.247 Am[21075:15203] [numberFormatter numberFromString:@"-$21.42"] is: (null)
2012-07-16 10:13:04.248 Am[21075:15203] [numberFormatter numberFromString:@"-$21.41"] is: -21.41
2012-07-16 10:13:04.248 Am[21135:15203] [numberFormatter numberFromString:@"-$21.40"] is: -21.4

0 个答案:

没有答案