Comment imprimer deux endroits avec le drapeau zéro dans une instruction d'impression?

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

  •  05-07-2019
  •  | 
  •  

Question

si vous voulez faire imprimer cette méthode en utilisant un pavé zéro, comment vous y prendrez

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
}
Était-ce utile?

La solution

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
}

Autres conseils

Vous pouvez utiliser java.util.Formatter ou String.format

String.format("%02d", somenumber)
for (int i=0; i<19; i++)
   System.out.format("i: %02d\n", i);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top