Java Decimal Format toString(double value, int precision)

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

Description

Thread-safe version of converting a double value to a String when only a certain number of digits are desired after a decimal point.

License

Apache License

Parameter

Parameter Description
value a parameter
precision a parameter

Declaration

public static String toString(double value, int precision) 

Method Source Code

//package com.java2s;
/*//from  ww  w.j  av a2  s .com
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * 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
 * 
 *      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.
 * #L%
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * Thread-safe version of converting a double value to a String when only
     * a certain number of digits are desired after a decimal point.
     * @param value
     * @param precision
     * @return
     */
    public static String toString(double value, int precision) {
        // create decimal format string
        StringBuilder buf = new StringBuilder("0");
        if (precision > 0) {
            buf.append(".");
        }
        for (int i = 0; i < precision; i++) {
            buf.append("0");
        }
        // create a decimal format
        DecimalFormat format = new DecimalFormat(buf.toString());
        return format.format(value);

        /**
        String temp = Double.toString(value);
        StringBuffer buf = new StringBuffer(temp.length()+precision);
        buf.append(temp);
        // find the '.' char
        int pos = buf.indexOf(".");
        // was the . found, if not, add it so we can use the same logic below
        if (pos < 0) {
            // if we want precision, add . and set the position correctly
            if (precision > 0) {
                buf.append('.');
                pos = buf.length()-1;   // set pos to last char
            } else {
                return buf.toString();
            }
        }
        // number of digits after
        int digitsAfter = buf.length() - pos - 1;

        // if precision is zero, then fake getting rid of the period by incrementing this
        if (precision == 0)
            digitsAfter++;

        // do we need to add, delete, or keep it?
        if (digitsAfter < precision) {
            // add zeoes
            int count = precision - digitsAfter;
            for (int i = 0; i < count; i++) {
                buf.append('0');
            }
        } else if (digitsAfter > precision) {
            // trim digits
            int count = digitsAfter - precision;
            buf.delete(buf.length()-count, buf.length());
        }
        return buf.toString();
         */
    }
}

Related

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