【发布时间】:2020-02-11 12:37:40
【问题描述】:
我的结果应该是这样的:$100 00,99
我设法根据需要格式化数字,但没有货币。
我设法单独获得货币,但无法将两者放在一起。
对于编号格式,我使用了DecimalFormatSymbol,就像对question 的回答一样。
private fun formatValue(value: Double, formatString: String): String {
val formatSymbols = DecimalFormatSymbols(Locale.ENGLISH)
formatSymbols.decimalSeparator = ','
formatSymbols.groupingSeparator = ' '
val formatter = DecimalFormat(formatString, formatSymbols)
return formatter.format(value)
}
formatValue(amount ,"###,###.00")
对于我使用此代码的货币:
fun getFormattedCurrency(currency: String, amount: Double): String {
val c = Currency.getInstance(currency)
val nf = NumberFormat.getCurrencyInstance()
nf.currency = c
return nf.format(amount)
}
如何将两者结合起来?
【问题讨论】:
-
如果你只是在末尾添加货币符号会不会更简单?如果您在某些 TextView 中显示金额,只需执行
myTextView.text = currency + amount.toString()之类的操作 -
别忘了货币是由 Locale 格式化的
标签: android formatting double decimal currency