Java Matrix to String matrixToString(double[][] matrix, int digit, String[] names)

Here you can find the source of matrixToString(double[][] matrix, int digit, String[] names)

Description

matrix To String

License

Open Source License

Declaration

public static String matrixToString(double[][] matrix, int digit, String[] names) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String matrixToString(double[][] matrix, int digit, String[] names) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < matrix.length; i++) {
            if (names != null)
                output.append(names[i]).append("\t");
            output.append(arrayToString(matrix[i], digit)).append("\n");
        }//from  w  w w.j  a v a 2s  . co m
        return output.toString();
    }

    public static String arrayToString(int[] array) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%d\t", array[i]));
        }
        output.append(String.format("%d", array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%.2f\t", array[i]));
        }
        output.append(String.format("%.2f", array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array, String format) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format(format + "\t", array[i]));
        }
        output.append(String.format(format, array[array.length - 1]));
        return output.toString();
    }

    public static String arrayToString(double[] array, int digit) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < array.length - 1; i++) {
            output.append(String.format("%." + digit + "f\t", array[i]));
        }
        output.append(String.format("%." + digit + "f", array[array.length - 1]));
        return output.toString();
    }
}

Related

  1. Array2DToString(double[][] array)
  2. array2DToString(int[][] grid)
  3. formatTable(String[][] table)
  4. matrix2string(int[][] matrix)
  5. matrix_toString(int[][] matrix)
  6. matrixToString(float[][][] matrix)
  7. matrixToString(int[][] m)
  8. matrixToString(int[][] matrix, String sep)
  9. matrixToString(String[][] m)