我在具有 NSNumberFormatterCurrencyStyle 和负最小值的 NSNumberFormatter 上遇到了 numberFromString 明显的一对一问题。从下面的测试中可以看出,numberFromString 在尝试转换最小金额时返回 nil,但在低于最小值 1 时正确返回。
我是否误解了“最小值”在负面上下文中的含义?或者这是 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