iphone - NSNumberFormatter 的乘数向下舍入而不是显示分数

标签 iphone ios nsnumberformatter

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

相关文章:

ios - 从包含的对象中调用UIViewController方法

ios - 更新到 Xcode 7.0.1 后上传 iOS 应用程序时出错

iphone - 比 NSNumberFormatterBehaviorDefault 更喜欢 NSNumberFormatterBehavior10_4 是个好主意吗?

iphone - iOS扩展UIWindow禁用了 Root View Controller ?

iPhone 位置按钮

ios - 如何在 WatchKit 应用程序中获取当前位置?

iphone - 是否可以在没有源代码的情况下将 .app 文件安装到 iPhone 模拟器上

cocoa - 将数字格式化为序数? (第一、第二等..)

objective-c - 防止小负数打印为 "-0"

javascript - 检查 JavaScript 是否支持 URL scheme