¿Cómo se imprimen dos lugares exactamente con el indicador de almohadilla cero en una declaración de impresión?

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

  •  05-07-2019
  •  | 
  •  

Pregunta

si desea realizar una impresión de este método utilizando el teclado cero, ¿cómo lo hace?

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
}
¿Fue útil?

Solución

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
}

Otros consejos

Puede usar java.util.Formatter o String.format

String.format("%02d", somenumber)
for (int i=0; i<19; i++)
   System.out.format("i: %02d\n", i);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top