Java: double: how to ALWAYS show two decimal digits

后端 未结 6 1968
悲&欢浪女
悲&欢浪女 2021-02-03 23:23

I use double values in my project and I would like to always show the first two decimal digits, even if them are zeros. I use this function for rounding and if the value I print

6条回答
  •  不知归路
    2021-02-03 23:44

    You can use something like this:

     double d = 1.234567;
     DecimalFormat df = new DecimalFormat("#.00");
     System.out.print(df.format(d));
    

    Edited to actually answer the question because I needed the real answer and this came up on google and someone marked it as the answer despite the fact that this wasn't going to work when the decimals were 0.

提交回复
热议问题