Java Decimal Format toString(double[] array)

Here you can find the source of toString(double[] array)

Description

to String

License

Open Source License

Declaration

public static String toString(double[] array) 

Method Source Code


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

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.TreeMap;

public class Main {
    private static final DecimalFormat df = new DecimalFormat("#.####");

    public static String toString(double[] array) {
        String display = "[";
        for (double val : array) {
            display += val + ", ";
        }/* ww w.jav a  2 s  . com*/
        display = display.substring(0, display.length() - 2);
        display += "]";
        return display;
    }

    public static String toString(List<String> list) {
        String display = "[";
        for (String val : list) {
            display += val + ", ";
        }
        if (display.length() > 2) {
            display = display.substring(0, display.length() - 2) + "]";
        } else {
            display += "]";
        }

        return display;
    }

    public static String toString(TreeMap<Double, Double> map) {
        String display = "[";
        for (Map.Entry<Double, Double> entry : map.entrySet()) {
            display += nice(entry.getKey()) + " : " + nice(entry.getValue()) + ", ";
        }
        display = display.substring(0, display.length() - 2) + "]";
        return display;
    }

    public static String toString(HashMap<String, Long> map) {
        String display = "[";
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            display += entry.getKey() + " : " + entry.getValue() + ", ";
        }
        display = display.substring(0, display.length() - 2) + "]";
        return display;
    }

    private static double nice(Double val) {
        return nice(val.doubleValue());
    }

    private static double nice(double val) {
        return Double.valueOf(df.format(val));
    }
}

Related

  1. toString(double d, int precision)
  2. toString(double value)
  3. toString(double value)
  4. toString(double value)
  5. toString(double value, int precision)
  6. toString(Double[] input)
  7. toString(double[][] a, int decimals)
  8. toString(final Double value)
  9. toString(final double value, final int maxDecimalPlaces)