這是我的代碼(好吧,其中一些)。我的問題是,我可以得到前9位數字,以前100表示,數字10 - 99表示前導0.Java System.out.print格式
我必須顯示所有的360每月付款,但如果我不所有月份的數字都是相同的長度,那麼我最終會得到一個輸出文件,它會繼續向右移動並抵消輸出的外觀。
System.out.print((x + 1) + " "); // the payment number
System.out.print(formatter.format(monthlyInterest) + " "); // round our interest rate
System.out.print(formatter.format(principleAmt) + " ");
System.out.print(formatter.format(remainderAmt) + " ");
System.out.println();
結果:
8 $951.23 $215.92 $198,301.22
9 $950.19 $216.95 $198,084.26
10 $949.15 $217.99 $197,866.27
11 $948.11 $219.04 $197,647.23
我想看到的是:
008 $951.23 $215.92 $198,301.22
009 $950.19 $216.95 $198,084.26
010 $949.15 $217.99 $197,866.27
011 $948.11 $219.04 $197,647.23
什麼其他的代碼,你需要從我的課,可以幫忙看看?
我所做的是: import java.text.DecimalFormat; 然後,我用Adam的建議進行了一點編輯。謝謝你,亞當! DecimalFormat newFormat = new DecimalFormat(「000」); System.out.print(newFormat.format(x + 1)+「」); > 008 $ 951.23 $ 215.92 $ 198,301.22 009 $ 950.19 $ 216.95 $ 198,084.26 010 $ 949.15 $ 217.99 $ 197,866.27 011 $ 948.11 $ 219.04 $ 197,647.23 – RazorSharp 2012-03-24 07:26:13