Java Double Number to String doubleToString(double value, int afterDecimalPoint)

Here you can find the source of doubleToString(double value, int afterDecimalPoint)

Description

Rounds a double and converts it into String.

License

Open Source License

Parameter

Parameter Description
value the double value
afterDecimalPoint the (maximum) number of digits permitted after the decimal point

Return

the double as a formatted string

Declaration

public staticString doubleToString(double value,
        int afterDecimalPoint) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  j  ava  2  s .  com*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.RoundingMode;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Main {
    /** Decimal format */
    private static final ThreadLocal<DecimalFormat> DF = new ThreadLocal<DecimalFormat>() {

        @Override
        protected DecimalFormat initialValue() {

            DecimalFormat df = new DecimalFormat();
            DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
            dfs.setDecimalSeparator('.');
            dfs.setNaN("NaN");
            dfs.setInfinity("Infinity");
            df.setGroupingUsed(false);
            df.setRoundingMode(RoundingMode.HALF_UP);
            df.setDecimalFormatSymbols(dfs);
            return df;
        }
    };

    /**
     * Rounds a double and converts it into String.
     * 
     * @param value the double value
     * @param afterDecimalPoint the (maximum) number of digits permitted after the
     *          decimal point
     * @return the double as a formatted string
     */
    public static/* @pure@ */String doubleToString(double value,
            int afterDecimalPoint) {

        DF.get().setMaximumFractionDigits(afterDecimalPoint);
        return DF.get().format(value);
    }

    /**
     * Rounds a double and converts it into a formatted decimal-justified String.
     * Trailing 0's are replaced with spaces.
     * 
     * @param value the double value
     * @param width the width of the string
     * @param afterDecimalPoint the number of digits after the decimal point
     * @return the double as a formatted string
     */
    public static/* @pure@ */String doubleToString(double value,
            int width, int afterDecimalPoint) {

        String tempString = doubleToString(value, afterDecimalPoint);
        char[] result;
        int dotPosition;

        if (afterDecimalPoint >= width) {
            return tempString;
        }

        // Initialize result
        result = new char[width];
        for (int i = 0; i < result.length; i++) {
            result[i] = ' ';
        }

        if (afterDecimalPoint > 0) {
            // Get position of decimal point and insert decimal point
            dotPosition = tempString.indexOf('.');
            if (dotPosition == -1) {
                dotPosition = tempString.length();
            } else {
                result[width - afterDecimalPoint - 1] = '.';
            }
        } else {
            dotPosition = tempString.length();
        }

        int offset = width - afterDecimalPoint - dotPosition;
        if (afterDecimalPoint > 0) {
            offset--;
        }

        // Not enough room to decimal align within the supplied width
        if (offset < 0) {
            return tempString;
        }

        // Copy characters before decimal point
        for (int i = 0; i < dotPosition; i++) {
            result[offset + i] = tempString.charAt(i);
        }

        // Copy characters after decimal point
        for (int i = dotPosition + 1; i < tempString.length(); i++) {
            result[offset + i] = tempString.charAt(i);
        }

        return new String(result);
    }
}

Related

  1. doubleToString(Double d, String format)
  2. doubleToString(double dnum)
  3. doubleToString(Double num)
  4. doubleToString(double val, int maxFractionDigits)
  5. doubleToString(double value)
  6. doubleToString(double value, int fractionDigits)
  7. doubleToString(double value, int maximum, int minimum)
  8. doubleToString(double value, String format)
  9. doubleToString(final double d, final int decimalPlaces)