Save an integer in two digit format in a variable in Java

后端 未结 4 793
闹比i
闹比i 2020-12-05 18:22

How can I store an integer in two digit format in Java? Like can I set

int a=01;

and print it as 01? Also, not only printing,

4条回答
  •  萌比男神i
    2020-12-05 19:03

    // below, %02d says to java that I want my integer to be formatted as a 2 digit representation
    String temp = String.format("%02d", yourIntValue);
    // and if you want to do the reverse
    int i = Integer.parse(temp);
    
    // 2 -> 02 (for example)
    

提交回复
热议问题