4

我已经配置了一个 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

我怎样才能停止错误的行为?

4

2 回答 2

3

试试这个代码。

cell.detailTextLabel.text = [[NSNumberFormatter euroCurrencyFormatter] stringFromNumber:[NSNumber numberWithDouble:[[breakdown valueForKey:@"VatAmount"] doubleValue]]]; // The VAT amount would be 304.

就像您将增值税作为整数传递一样,您将始终以 int 格式获得该数字。

于 2012-01-23T12:03:14.113 回答
0

请参阅NSNumberFormatter 参考的“配置舍入行为” 。

于 2012-01-23T11:36:53.300 回答