Java Decimal Format listToString(List list)

Here you can find the source of listToString(List list)

Description

list To String

License

Apache License

Declaration

public static String listToString(List<Double> list) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.List;

public class Main {
    protected static final NumberFormat formatter = new DecimalFormat("###.###");

    public static String listToString(List<Double> list) {
        if (list.isEmpty()) {
            return "[]";
        }//from ww w.j ava 2s .  c om
        StringBuilder str = new StringBuilder();
        str.append("[").append(formatDouble(list.get(0)));
        for (int i = 1; i < list.size(); i++) {
            str.append(", ").append(formatDouble(list.get(i)));
        }
        str.append("]");
        return str.toString();
    }

    public static String formatDouble(double value) {
        return formatter.format(value);
    }

    public static String formatDouble(double value, int n) {
        StringBuilder str = new StringBuilder("###.");
        for (int nn = 0; nn < n; nn++) {
            str.append("#");
        }
        NumberFormat newFormatter = new DecimalFormat(str.toString());
        return newFormatter.format(value);
    }
}

Related

  1. isDouble(String _str)
  2. isLikelyDouble(long value)
  3. isprime(double x)
  4. isValidDouble(String value, int decimals)
  5. Julian_Cal(double JDN)
  6. numToString(double num)
  7. pad(double n)
  8. padding(final double number)
  9. padToMinWidth(double num, int minWidth)