Print Integer with 2 decimal places in Java

后端 未结 7 914
轻奢々
轻奢々 2020-12-16 15:10

in my code i use integers multiplied by 100 as decimals (0.1 is 10 etc). Can you help me to format output to show it as decimal?

7条回答
  •  無奈伤痛
    2020-12-16 15:46

    I would say to use 0.00 as format:

          int myNumber = 10;
          DecimalFormat format = new DecimalFormat("0.00"); 
          System.out.println(format.format(myNumber));
    

    It will print like:           

          10.00
    

    The advantage here is:

    If you do like:

          double myNumber = .1;
          DecimalFormat format = new DecimalFormat("0.00"); 
          System.out.println(format.format(myNumber));
    

    It will print like:

          0.10
    

提交回复
热议问题