NumberFormatter在货币模式下对小的负数的不正确行为

问题描述 投票:2回答:1

[使用NumberFormatter并将numberStyle设置为.currency时,格式化程序将所有数字四舍五入到小数点后两位。但是,对于较小的负数,其值四舍五入到小数点后两位为0(数字> = -0.005),输出字符串包含负号,因此成为-$0.00而不是$0.00

是否有办法仅通过切换NumberFormatter的某些属性来更改此行为,而不必使用替代方法?目前,我自己将值四舍五入到小数点后两位,检查输出是否为-0并采取相应措施。但是,最好将NumberFormatter设置为不区分-00

下面的代码显示了问题以及我目前的解决方法(可以在操场上进行测试)-我知道Locale应该基于currencySymbol进行设置,但这只是展示问题的代码,而不是生产代码代码):

public class CurrencyFormatter {

    private let formatter: NumberFormatter

    public init(locale: Locale = Locale.current) {
        let formatter = NumberFormatter()
        formatter.locale = locale
        formatter.numberStyle = .currency
        self.formatter = formatter
    }

    // Adds currency symbol and returns a string e.g. input 1 output "£1"
    public func formatWithCurrencySymbol(value: Decimal, currencyCode: String) -> String? {
        formatter.currencyCode = currencyCode

        // Workaround for cases when the currency behaviour rounds small negative values (<0.0051) to -0.00, where we don't want to have a - sign
        var value = value
        if value < 0 {
            let roundedValue = round(Double(truncating: value as NSDecimalNumber) * 100.0)
            if roundedValue == 0 && roundedValue.sign == .minus {
                value = 0
            }
        }

        return formatter.string(for: value)
    }

    public func formatWithCurrencySymbolNoWorkaround(value: Decimal, currencyCode: String) -> String? {
        formatter.currencyCode = currencyCode
        return formatter.string(for: value)
    }
}

let formatter = CurrencyFormatter()

formatter.formatWithCurrencySymbol(value: 0.01, currencyCode: "USD") // "$0.01"
formatter.formatWithCurrencySymbol(value: -0.001, currencyCode: "EUR") // "€0.00"
formatter.formatWithCurrencySymbol(value: -0.0002, currencyCode: "EUR") // "€0.00"
formatter.formatWithCurrencySymbol(value: -0.01, currencyCode: "EUR") // "-€0.01"

formatter.formatWithCurrencySymbol(value: 0.01, currencyCode: "USD") // "$0.01"
formatter.formatWithCurrencySymbol(value: -0.001, currencyCode: "EUR") // "-€0.00"
formatter.formatWithCurrencySymbol(value: -0.0002, currencyCode: "EUR") // "-€0.00"
formatter.formatWithCurrencySymbol(value: -0.01, currencyCode: "EUR") // "-€0.01"
formatter.formatWithCurrencySymbolNoWorkaround(value: -0.005, currencyCode: "EUR") // "-€0.00"

[使用NumberFormatter并将numberStyle设置为.currency时,格式化程序将所有数字四舍五入到小数点后两位。但是,对于较小的负数,其值四舍五入为... ...

swift nsnumberformatter
1个回答
0
投票

您可以添加简单的逻辑来检查该值是否小于-0.01,如果是,则返回绝对值。

© www.soinside.com 2019 - 2024. All rights reserved.