Java – how to print two positions correctly using the zero padding flag in a print statement

What if you want to print with zero pad

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
}

Solution

int month,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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>