Java Double Number to String doubleToStr(double origin, int decimals)

Here you can find the source of doubleToStr(double origin, int decimals)

Description

Formats a double to produce a string with a number of fraction digits.

License

Apache License

Parameter

Parameter Description
origin double to format
decimals number of fraction digits

Return

Formatted string.

Declaration

public static String doubleToStr(double origin, int decimals) 

Method Source Code

//package com.java2s;
/**/*from  ww w.j a v  a  2 s .  c om*/
 * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
 *
 * 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.
 */

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

import java.util.Locale;

public class Main {
    /**
     * Formats a double to produce a string with a number of fraction
     * digits.
     *
     * @param origin   double to format
     * @param decimals number of fraction digits
     * @return Formatted string.
     */
    public static String doubleToStr(double origin, int decimals) {
        return getFormat(decimals).format(new Double(origin));
    }

    /**
     * Formats a Number to produce a string with a number of fraction
     * digits.
     *
     * @param origin   Number to format
     * @param decimals number of fraction digits
     * @return Formatted string.
     */
    public static String doubleToStr(Number origin, int decimals) {
        return getFormat(decimals).format(origin);
    }

    /**
     * Returns the decimal formatter, which has a number of decimals.
     *
     * @param decimals number of decimals
     * @return desired DecimalFormat
     */
    private static DecimalFormat getFormat(int decimals) {
        DecimalFormat df;
        df = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
        DecimalFormatSymbols dsym = df.getDecimalFormatSymbols();
        char decimalSeparator = dsym.getDecimalSeparator();

        StringBuffer pattern = new StringBuffer("0");
        pattern.append(decimalSeparator);
        for (int i = 0; i < decimals; i++)
            pattern.append("0");

        DecimalFormat numberFormat = new DecimalFormat(pattern.toString());
        DecimalFormatSymbols decimalSymb;
        decimalSymb = numberFormat.getDecimalFormatSymbols();

        numberFormat.setGroupingSize(50);
        decimalSymb.setDecimalSeparator(decimalSeparator);
        numberFormat.setDecimalFormatSymbols(decimalSymb);

        return numberFormat;
    }
}

Related

  1. doubleToCoreString(double number)
  2. doubleToDollarString(double val)
  3. doubleToFixedString(double d, int length)
  4. doubleToScientificString(double number, int fractionDigits)
  5. doubleToStr(double d)
  6. doubleToStr1(double d)
  7. doubleToStrFormatado(Double valor)
  8. doubleToString(double d)
  9. doubleToString(double d, int df)