我已经配置了一个 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 欧元
我怎样才能阻止错误的行为?
最佳答案
试试这段代码。
cell.detailTextLabel.text = [[NSNumberFormatter euroCurrencyFormatter] stringFromNumber:[NSNumber numberWithDouble:[[breakdown valueForKey:@"VatAmount"] doubleValue]]]; // The VAT amount would be 304.
就像您将 VAT 作为整数传递一样,您将始终获得 int 格式的数字。
关于iphone - NSNumberFormatter 的乘数向下舍入而不是显示分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970954/