Swift 显示价格取决于区域设置

Swift show price depends on Locale

我得到一个代表价格的字符串值。我想将它转换为另一个具有特定精度的字符串,并根据当前设备区域设置对其进行格式化。

Decimal(string: "123,4567", locale: NSLocale.current)?.formatted(.number.precision(.fractionLength(precision)))

此代码适用于德语,输出为 "123,45"。但是如果我切换到英语,输出是 "123.00"。问题在于点而不是逗号。任何想法如何修复它并显示正确的数字 "123.45"?

用于输入字符串的区域设置必须与字符串的格式相匹配,因此如果它是德语格式,则使用德语区域设置

let locale = Locale(identifier: "de_DE")
let string = Decimal(string: "123,4567", locale: locale)?
    .formatted(.number.precision(.fractionLength(precision)))

这使用 Locale.current(本例中为瑞典语)作为输出

123,46

因为这是价格,所以这里是货币示例

let string = Decimal(string: "123,4567", locale: .current)?
    .formatted(.currency(code: "EUR")
        .precision(.fractionLength(2))
        .rounded(rule: .down)
        .locale(.current))

€123,45

如果您只需要格式化要使用特定货币显示的小数,那么您需要 Decimal.FormatStyle.Currency。会自动丢弃多余的精度长度:

let valueString = "1234,5678"
let decimalLocale: Locale = .init(identifier: "de_DE")
let currencyLocale: Locale = .init(identifier: "en_US")  // or current (used US locale just for demonstration purposes)
if let decimal = Decimal(string: valueString, locale: decimalLocale),
    let currencyCode = currencyLocale.currencyCode {
    let currencyStyle: Decimal.FormatStyle.Currency = .init(code: currencyCode, locale: currencyLocale)
    let currencyFormatted = decimal.formatted(
        currencyStyle.rounded(rule: .towardZero)
    )
    print(currencyFormatted)  // ",234.56\n"
}

edit/update:

我通常不喜欢操纵字符串,但如果您的源代码没有特定格式,有时小数点分隔符是逗号,有时是句点,您需要在将其转换为之前用句点替换字符串逗号十进制:

let valueString = "1234,5678"
let currencyLocale: Locale = .init(identifier: "en_US")  // or current (used US locale just for demonstration purposes)
if let decimal = Decimal(string: valueString.replacingOccurrences(of: ",", with: ".")),
    let currencyCode = currencyLocale.currencyCode {
    let currencyStyle: Decimal.FormatStyle.Currency = .init(code: currencyCode, locale: currencyLocale)
    let currencyFormatted = decimal.formatted(
        currencyStyle.rounded(rule: .towardZero)
    )
    print(currencyFormatted)  // ",234.56\n"
}