Java Decimal Format toString(double d, int precision)

Here you can find the source of toString(double d, int precision)

Description

to String

License

Apache License

Declaration

public static String toString(double d, int precision) 

Method Source Code

//package com.java2s;
/**************************************************************************************
Copyright 2015 Applied Research Associates, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the License
at:/*from  w w w .  j  a  v  a2  s  .c o m*/
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
**************************************************************************************/

import java.text.DecimalFormat;
import java.util.*;

public class Main {
    private static Map<Integer, DecimalFormat> formatters = new HashMap<Integer, DecimalFormat>();

    public static String toString(double d, int precision) {
        // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly
        StringBuffer sb = new StringBuffer();
        if (precision > 0) {
            DecimalFormat df = getFormatter(precision);
            sb.append(df.format(d));
        } else {
            sb.append(d);
        }
        return sb.toString();
    }

    public static String toString(double[] data) {
        return toString(data, -1);
    }

    public static String toString(double[] data, int precision) {
        // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly
        StringBuffer sb = new StringBuffer();
        if (precision > 0) {
            DecimalFormat df = getFormatter(precision);

            for (int i = 0; i < data.length; i++) {
                sb.append(df.format(data[i]));
                if (i < data.length - 1)
                    sb.append(",");
            }
        } else {
            for (int i = 0; i < data.length; i++) {
                sb.append(data[i]);
                if (i < data.length - 1)
                    sb.append(",");
            }
        }
        return sb.toString();
    }

    public static String toString(List<Double> data) {
        return toString(data, -1);
    }

    public static String toString(List<Double> data, int precision) {
        int idx = 0;
        // TODO DecimalFormat is a bit slow, check out Java Performance Tuning from O'Reilly
        StringBuffer sb = new StringBuffer();
        if (precision > 0) {
            DecimalFormat df = getFormatter(precision);

            for (Double d : data) {
                sb.append(df.format(d));
                if (idx++ != data.size())
                    sb.append(",");
            }
        } else {
            for (Double d : data) {
                sb.append(d);
                if (idx++ != data.size())
                    sb.append(",");
            }
        }
        return sb.toString();
    }

    public static DecimalFormat getFormatter(int precision) {
        DecimalFormat df = formatters.get(precision);
        if (df == null) {
            StringBuffer format = new StringBuffer("#0.");
            for (int i = 0; i < precision; i++)
                format.append("#");
            df = new DecimalFormat(format.toString());
            formatters.put(precision, df);
        }
        return df;
    }
}

Related

  1. toScientific(double I, int digits)
  2. toSplitDecimalString(Double num, int decimal)
  3. toStockPrice(double value)
  4. toString(double d)
  5. toString(double d)
  6. toString(double value)
  7. toString(double value)
  8. toString(double value)
  9. toString(double value, int precision)