How to display a number with always 2 decimal points using BigDecimal?

后端 未结 6 1336
北荒
北荒 2020-12-06 04:00

I am using BigDecimal to get some price values. Requirement is something like this, what ever the value we fetch from database, the displayed valued should have 2 d

6条回答
  •  太阳男子
    2020-12-06 04:27

    To format numbers in JAVA you can use:

     System.out.printf("%1$.2f", d);
    

    where d is your variable or number

    or

     DecimalFormat f = new DecimalFormat("##.00");  // this will helps you to always keeps in two decimal places
     System.out.println(f.format(d)); 
    

提交回复
热议问题