Как вы печатаете два места точно, используя флаг нуля в выражении печати - PullRequest
7 голосов
/ 26 ноября 2009

если вы хотите, чтобы этот метод печатался с использованием нуля, как вы это делаете

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 ]

9 голосов
/ 26 ноября 2009
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
}
5 голосов
/ 26 ноября 2009

Можно использовать java.util.Formatter или String.format

String.format("%02d", somenumber)
2 голосов
/ 26 ноября 2009
for (int i=0; i<19; i++)
   System.out.format("i: %02d\n", i);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...