2015-12-10 43 views
0

我必須將雙精度值切分爲數字,但我不知道如何使用表中的值來完成此操作。我需要從0.254300這個0.2543中獲得。 這是我的代碼,所以如何削減額外的零。在表格中將雙精度值切分爲4位數

public class Diagonal{ 
public static void main(String[] args){ 


    double [][] tab = new double [5][5]; 


    for (int i = 0; i < 5; i++){ 
     for (int j = 0; j < 5; j++){ 
      tab[i][j] = (int) (Math.random() * 9000)/10000.0; 

     } 
    } 
    for(int i = 0; i < 5; i++){ 
     for (int j = 0; j < 5; j++) 
      if(i==j || i+j==4) 


       System.out.printf("%4f", tab[i][j]); 
      else 
       System.out.printf("%4c",' '); 

      System.out.println(); 

     }     
    } 
} 

回答

0

如果它應該是打印這樣的事情:

0,4435     0,6241 
     0,2373  0,5890  
      0,2100    
     0,4276  0,4411  
0,4157     0,3537 

你應該使用:

if(i==j || i+j==4) 
    System.out.printf("%.4f", tab[i][j]); 
else 
    System.out.printf("%6c",' '); 
+0

這就是我需要的。只是一點點開關,謝謝 – Johny

相關問題