Double decimal formatting in Java

后端 未结 14 1219
囚心锁ツ
囚心锁ツ 2020-11-22 05:17

I\'m having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it\'s 4.00 instead?

14条回答
  •  伪装坚强ぢ
    2020-11-22 05:46

    One of the way would be using NumberFormat.

    NumberFormat formatter = new DecimalFormat("#0.00");     
    System.out.println(formatter.format(4.0));
    

    Output:

    4.00

提交回复
热议问题