Formatting numbers using DecimalFormat

后端 未结 6 874
我寻月下人不归
我寻月下人不归 2021-01-01 21:05

I am trying to format prices using DecimalFormat, but this isn\'t working for all variations.

DecimalFormat df = new DecimalFormat(\"0.##\")
df.format(7.8)
d         


        
6条回答
  •  悲&欢浪女
    2021-01-01 21:35

    Use the BigDecimal number class instead:

    e.g. if n is a BigDecimal, then you can use

    String s = NumberFormat.getCurrencyInstance().format(n);
    

    By the way, it's best practice to use BigDecimal when working with money.

提交回复
热议问题