Im製作從1-10的分割表結果範圍從整數到xx到x.xx我使用了十進制格式來保持數字到2位小數,但是拋出了格式桌子。我將如何打印號碼,這樣他們都將是X.XX如何格式化打印的雙打
for example
1 -> 1.00
0.5 -> 0.50
0.333... -> 0.33
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(" 1 2 3 4 5 6 7 8 9 10");
for(double i=1;i<=10;i++){
System.out.print(df.format(i)+" ");
if (i<10) System.out.print(" ");
else System.out.print("");
for(double j=1;j<=10;j++){
double div=(j/i);
System.out.print(df.format(div));
if (div < 10) System.out.print(" ");
}
System.out.println();
this prints
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
3 0.33 0.67 1 1.33 1.67 2 2.33 2.67 3 3.33
4 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5
5 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
6 0.17 0.33 0.5 0.67 0.83 1 1.17 1.33 1.5 1.67
7 0.14 0.29 0.43 0.57 0.71 0.86 1 1.14 1.29 1.43
8 0.12 0.25 0.38 0.5 0.62 0.75 0.88 1 1.12 1.25
9 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1 1.11
10 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
i want to print everything as x.xx or neatly in columns
1 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.0
2 0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 4.50 5.00
3 0.33 0.67 1.00 1.33 1.67 2.00 2.33 2.67 3.00 3.33
etc
幽州。請解決這個問題。 – 2014-10-11 02:19:22