Fixed decimal numbers with JAVA

前端 未结 4 2014
感动是毒
感动是毒 2021-01-13 05:10

How do I represent numbers in the format

001 = 1
002 = 2
003 = 3
010 = 10
020 = 20

The number of digits is always 3.

4条回答
  •  迷失自我
    2021-01-13 05:57

    If you want to output integers such that they always have leading zeros, use String.format with a zero before the number of digits in the format string, for example:

    int i = 3;
    String s = String.format("%03d", i);
    System.out.println(s);
    

    Gives the output:

    003
    

提交回复
热议问题