如何打印格式化的 BigDecimal 值? [英] How to print formatted BigDecimal values?
本文介绍了如何打印格式化的 BigDecimal 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 BigDecimal
字段 amount
代表钱,我需要在浏览器中以类似 $123.00
的格式打印它的值,$15.50
,$0.33
.
I have a BigDecimal
field amount
which represents money, and I need to print its value in the browser in a format like $123.00
, $15.50
, $0.33
.
我该怎么做?
(我自己看到的唯一简单的解决方案是从 BigDecimal
获取 floatValue
,然后使用 NumberFormat
为分数部分).
(The only simple solution which I see myself is getting floatValue
from BigDecimal
and then using NumberFormat
to make two-digit precision for the fraction part).
推荐答案
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
它将使用您的 JVM 当前默认的 Locale
以选择您的货币符号.或者您可以指定Locale
.
It will use your JVM’s current default Locale
to choose your currency symbol. Or you can specify a Locale
.
NumberFormat.getInstance(Locale.US)
有关详细信息,请参阅 NumberFormat
类.
For more info, see NumberFormat
class.
这篇关于如何打印格式化的 BigDecimal 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文