Как вы печатаете два места точно, используя флаг нуля в выражении печати

StackOverflow https://stackoverflow.com/questions/1803670

  •  05-07-2019
  •  | 
  •  

Вопрос

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

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top