我必须从法国数字字符串中获取数字:@“30.000,00” 当我使用NSNumberFormattern来解析
时 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSNumber *priceNumberValue = [numberFormatter numberFromString:@30.000"];
它返回30,我的预期回报是30000 我尝试使用其他选项
NSLocale *vnLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
[numberFormatter setLocale:vnLocale];
或
[numberFormatter setDecimalSeparator:@","];
但仍会返回意外值:30
我告诉NSNumberFormatter解析@“30.000,00”的正确方法是什么?
答案 0 :(得分:3)
设置区域设置应该有效,但您必须告诉numberformatter使用分组分隔符:
[numberFormatter setUsesGroupingSeparator:YES];
如果这不起作用,以下一定会起作用:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setDecimalSeparator:@","];
[numberFormatter setGroupingSeparator:@"."];
[numberFormatter setUsesGroupingSeparator:YES];
NSNumber *priceNumberValue = [numberFormatter numberFromString:@"30.000,00"];
检查完后,似乎“fr-FR”语言环境没有您期望的属性。但是,使用“fr-BE”语言环境可以正常工作:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_BE"];
[numberFormatter setLocale:locale];
[locale release];
[numberFormatter setUsesGroupingSeparator:YES];
NSNumber *priceNumberValue = [numberFormatter numberFromString:@"30.000,00"];