如果我使用以下代码:不正确的结果值
- (NSString*)formatToCurrency:(float)inFloat {
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:2];
[currencyFormatter setLocale:[NSLocale currentLocale]];
NSNumber *x = [NSNumber numberWithFloat:inFloat];
return [NSString stringWithFormat:@" %@", [currencyFormatter stringFromNumber:x]];
}
然后调用代码时传递203500,收到输出:$ 203,500.02。
我很想听听关于正在发生的事情的一些想法。
感谢,
马特
编辑:XCode中被报告203500当我使用调试器观看它,但是当我转换的浮到一个NSString,它也将显示203500.018。这意味着问题出现在浮点数中,而不是在NSNumberFormatter中。
我不能再现这个结果,我得到'203,500.00'。 – dasblinkenlight
举一个@vikingosegundo的例子..参见http://stackoverflow.com/questions/856019/localize-currency-for-iphone –
再试一次,只是将所有出现的'float'改为'double'。 –