How do you print two places exactly using zero-pad flag in a print statement
Question
if wanted to make a this method print using zero pad how do you do so
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;
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
}
OTHER TIPS
You can use java.util.Formatter or String.format
String.format("%02d", somenumber)
for (int i=0; i<19; i++)
System.out.format("i: %02d\n", i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow