Java Double Number Format toString(double[] arr, NumberFormat nf)

Here you can find the source of toString(double[] arr, NumberFormat nf)

Description

Copies the given array, using a standard scientific notation number formatter and beginning each line with the given lineInit.

License

Open Source License

Parameter

Parameter Description
arr The double array to turn into a string.
nf The number format to use.

Return

The formatted string.

Declaration

public static String toString(double[] arr, NumberFormat nf) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by      //

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**//from  www .  j  a v  a  2  s. c om
     * Copies the given array, using a standard scientific notation number
     * formatter and beginning each line with a tab character. The number format
     * is DecimalFormat(" 0.0000;-0.0000").
     *
     * @param arr The double array to turn into a String.
     * @return The formatted array.
     */
    public static String toString(double[] arr) {
        NumberFormat nf = new DecimalFormat(" 0.0000;-0.0000");
        return toString(arr, nf);
    }

    /**
     * Copies the given array, using a standard scientific notation number
     * formatter and beginning each line with a tab character. The number format
     * is DecimalFormat(" 0.0000;-0.0000").
     *
     * @param arr The int array to turn into a string.
     * @return The formatted array.
     */
    public static String toString(int[] arr) {
        StringBuilder buf = new StringBuilder();
        buf.append("\n");
        for (int anArr : arr) {
            buf.append(anArr).append("\t");
        }
        return buf.toString();
    }

    /**
     * Copies the given array, using a standard scientific notation number
     * formatter and beginning each line with the given lineInit. The number
     * format is DecimalFormat(" 0.0000;-0.0000").
     *
     * @param arr The double array to turn into a string.
     * @param nf  The number format to use.
     * @return The formatted string.
     */
    public static String toString(double[] arr, NumberFormat nf) {
        String result;
        if (nf == null) {
            throw new NullPointerException("NumberFormat must not be null.");
        }
        if (arr == null) {
            result = nullMessage();
        } else {
            StringBuilder buf = new StringBuilder();
            buf.append("\n");
            buf.append("\t");

            for (double anArr : arr) {
                buf.append(nf.format(anArr)).append("\t");
            }
            result = buf.toString();
        }
        return result;
    }

    private static String nullMessage() {
        StringBuilder buf = new StringBuilder();
        buf.append("\n");
        buf.append("\t");
        buf.append("<Matrix is null>");
        return buf.toString();
    }
}

Related

  1. prettyDecimalFormat(double d, int numPlaces)
  2. roundFormat(double d, int i)
  3. shorten(double num)
  4. toDoubleFromHumanFormat(String doubleAsString)
  5. toString(double in, String format)
  6. toString(double[] y, Format format)