我怎样才能让 NumberFormat 使用小数 cents/pennies 的货币?

How can I get NumberFormat to use a currency but with fractional cents/pennies?

我想使用 NumberFormatDouble 转换为 String。我目前使用的是基于货币区域设置的 NumberFormat,但这会将 Double 舍入到小数点后两位。我怎样才能让它不四舍五入货币以包含小数便士?

所以如果我有

val doubleFormatMoney = 1.032323135

val format = NumberFormat.getCurrencyInstance(Locale.US)

val stringFormatMoney = format.format(doubleFormatMoney)

那么我们应该有 stringFormatMoney = .032323135 但是我得到 stringFormatMoney = .03

每个语言区域的货币格式规范包括要使用的小数位数,因为这因货币而异(某些货币使用 3dp,例如约旦第纳尔)。因此 NumberFormat.getCurrencyInstance(Locale.US) 根据定义包括四舍五入到 2dp。

如果您想使用标准 Double toString 加上 Locale 的货币符号,您可以这样做:

Currency.getInstance(Locale.US).getSymbol(Locale.US) + doubleFormatMoney