我已经配置了一个 NSNumberFormatter 来将在 NSDictionary 中存储为美分的金额转换为欧元。因为它们存储为美分,所以我将格式化程序设置multiplier
为[NSNumber numberWithDouble:0.01]
. 但是,当我尝试将 304 欧分显示为欧元时,我得到了€ 3,00
.
这让我相信乘数是在进行整数除法而不是双除法。
NSFormatter 配置
/**
Returns an NSNumberFormatter that can be used to display currency in euro's (as determined in The Netherlands).
*/
+ (NSNumberFormatter *)euroCurrencyFormatter
{
static NSNumberFormatter *numberFormatter = nil;
@synchronized(self) {
if (!numberFormatter) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setMultiplier:[NSNumber numberWithDouble:0.01]];
NSLocale *nlLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"];
[numberFormatter setLocale:nlLocale];
[nlLocale release];
}
}
return numberFormatter;
}
调用 NSFormatter
cell.detailTextLabel.text = [[NSNumberFormatter euroCurrencyFormatter] stringFromNumber:[breakdown valueForKey:@"VatAmount"]]; // The VAT amount would be 304.
结果
€ 3,00
我怎样才能停止错误的行为?