我们的质量保证团队会发现缺陷。当他们写0.095
或0,095
在货币字段中时,NSNumberFormatter
将内容转换为95
.
这是我们正在使用的代码:
+ (NSNumber *) getNumberWithDecimalFromString:(NSString*) theSource
{
if ([TSCommons isEmpty:theSource])
return [[NSNumber alloc] initWithInt:0];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
if ([theSource rangeOfString:@","].location != NSNotFound) {
[formatter setDecimalSeparator:@","];
[formatter setCurrencyDecimalSeparator:@"."];
} else if ([theSource rangeOfString:@"."].location != NSNotFound) {
[formatter setDecimalSeparator:@"."];
[formatter setCurrencyDecimalSeparator:@"."];
}
NSNumber *tmp = [formatter numberFromString:theSource];
[formatter release];
return [tmp retain];
}
语言环境是es_ES
对这种意外行为有任何想法吗?
介绍更多关于这个问题的信息,因为上次XCode更新后又回来我准备了一个测试
- (void) testNumberFormatter
{
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,1"] doubleValue]);
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,12342"] doubleValue]);
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,12"] floatValue]);
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,123"] floatValue]);
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,1234"] floatValue]);
NSLog(@"%f",[[TSCommons getNumberWithDecimalFromString:@"41,1234234"] doubleValue]);
}
控制台上的结果如下:
41.100000
41.123420
41.119999
41123.000000
41.123402
41.123423
谢谢!!