不同的NSNumberFormatter行为

时间:2010-03-09 15:38:40

标签: iphone cocoa cocoa-touch

考虑以下代码:

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时出错了吗?或者这是两个平台之间的差异吗?

1 个答案:

答案 0 :(得分:0)

NSString *s = @"US$ 0,05";

如果删除空格怎么办?