Convert 1 to 01

前端 未结 11 1463
逝去的感伤
逝去的感伤 2021-02-07 04:09

I have an int between 1 - 99. How do I get it to always be a double digit, ie: 01, 04, 21?

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-07 05:09

    I think, I should add the link to the official Java documentation on formatting numeric outputs.

    https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

    Sample Snipplet: Prints 00 up to 100

    class Main {
        public static void main(String[] args) {
          for(int i=0;i<=100;i++){
            System.out.println(String.format("%02d", i)); 
          }
        }
    }
    

    Output:

    00
    01
    ...
    10
    ...
    100
    

提交回复
热议问题