0

I have to cut double values to for digits, but i don't know how to do it with values from tables. i need to get from 0.254300 this 0.2543. This is my code, so how to cut extra zeros.

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();

      }                 
    }
 }
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
Johny
  • 15
  • 3

1 Answers1

0

If it is supposed to print something like this:

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

You should use:

if(i==j || i+j==4)
    System.out.printf("%.4f", tab[i][j]);
else
    System.out.printf("%6c",' ');
ArcticLord
  • 3,999
  • 3
  • 27
  • 47