2012-10-22 40 views

回答

15

假設,由於您知道用於格式化double,所以您至少知道了關於formatting

所以,適當地格式化您的整數,你可以用0開頭前面加上0%2d墊之前,你的號碼: -

int i = 9; 
System.out.format("%02d\n", i); // Will print 09 
1

「寫」如何?假設你的意思是從PrintStream開始,那麼你可以使用printf。格式詳細信息here。我相信格式字符串將是%02d

14

使用的String.format方法...

示例

System.out.println(String.format("%02d", 5)); 
    System.out.println(String.format("%02d", 55)); 
    System.out.println(String.format("%02d", 15));