Java - format double value as dollar amount

前端 未结 5 863
故里飘歌
故里飘歌 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:43

    Use BigDecimal instead of double for currency types. In Java Puzzlers book we see:

    System.out.println(2.00 - 1.10);
    

    and you can see it will not be 0.9.

    String.format() has patterns for formatting numbers.

提交回复
热议问题