【发布时间】:2019-10-21 15:05:34
【问题描述】:
我有以下方法来构建货币格式化程序。有 2 个输入字段,区域设置和货币:
private fun getCurrencyDecimalFormat(locale: Locale, currency: Currency): DecimalFormat {
val currencyFormat = NumberFormat.getCurrencyInstance(locale) as DecimalFormat
currencyFormat.positivePrefix = currencyFormat.positivePrefix + " "
currencyFormat.roundingMode = RoundingMode.DOWN
val symbol = currency.getSymbol(locale)
val decimalFormatSymbols = currencyFormat.decimalFormatSymbols
decimalFormatSymbols.currencySymbol = symbol
currencyFormat.decimalFormatSymbols = decimalFormatSymbols
currencyFormat.isParseBigDecimal = true
return currencyFormat
}
它是这样称呼的:
val currencyFormat = getCurrencyDecimalFormat(locale, currency)
return currencyFormat.format(amount)
当货币输入与语言环境输入的货币相同时,它可以正常工作,所以:
- 语言环境:es_ES,货币:EUR = 0,00 € -> OK
- 语言环境:en_US,货币:USD = $ 0.00 -> OK
但如果我们有以下情况,那就错了:
- 语言环境:es_ES,货币:USD = 0,00 $ -> KO
- 语言环境:en_US,货币:EUR = $ 0.00 -> KO
似乎货币设置不正确......有什么想法吗?我做错了什么?
【问题讨论】:
标签: kotlin currency number-formatting decimalformat currency-formatting