考虑这段代码:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterCurrencyStyle;
NSLocale *l = [[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"];
nf.locale = l;
[l release];
nf.decimalSeparator = @",";
nf.currencySymbol = @"US$";
[nf setLenient:YES];
NSString *s = @"US$ 0,05";
double d = [[nf numberFromString:s] doubleValue];
NSLog(@"%.2f",d);`
如果我在普通的基于 Cocoa 控制台的应用程序 (10.6 sdk) 中执行此操作,我会在调试控制台中得到输出“0.05”。
但是,如果我在 iPhone 环境 (OS 3.1.3) 中执行此操作,我会在调试控制台中得到“0.00”输出。我使用 NSNumberFormatter 是否做错了什么?或者这是两个平台之间的差异?
最佳答案
NSString *s = @"US$ 0,05";
如果删除空格会怎样?
关于iphone - 不同的 NSNumberFormatter 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2410244/