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

时间:2009-11-26 13:28:51

标签: java printf

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

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
}

3 个答案:

答案 0 :(得分:9)

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
}

答案 1 :(得分:5)

您可以使用java.util.FormatterString.format

String.format("%02d", somenumber)

答案 2 :(得分:2)

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