Java - format double value as dollar amount

前端 未结 5 866
故里飘歌
故里飘歌 2020-12-01 14:03

I need to format the double \"amt\" as a dollar amount println(\"$\" + dollars + \".\" + cents) such that there are two digits after the decimal.

What is the best wa

5条回答
  •  我在风中等你
    2020-12-01 14:23

    You can use a DecimalFormat

    DecimalFormat df = new DecimalFormat("0.00");
    System.out.println(df.format(amt));
    

    That will give you a print out with always 2dp.

    But really, you should be using BigDecimal for money, because of floating point issues

提交回复
热议问题