如果我使用以下代码:
- (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]];
}
Run Code Online (Sandbox Code Playgroud)
然后在调用代码时通过203500,我收到输出:$ 203,500.02.
我很想听听有关正在发生的事情的一些想法.
谢谢,
马特
编辑:当我用调试器查看时,XCode报告了203500,但是当我将浮点数转换为NSString时,它也会显示203500.018.这意味着问题出在浮点数而不是NSNumberFormatter中.