如何在print语句中使用零填充标志准确打印两个位置

如果想使用零垫打印此方法,您如何这样做

int month, day; public void printNumeric() { System.out.printf("month +"/" +day +" \n"); // i would like the month if it is 5 to be 05 same thing with the day } 

 int month, day; public void printNumeric() { System.out.printf("%02d/%02d\n", month, day); // i would like the month if it is 5 to be 05 same thing with the day } 

您可以使用java.util.Formatter或String.format

 String.format("%02d", somenumber) 
 for (int i=0; i<19; i++) System.out.format("i: %02d\n", i);