Java Fraction Format formatDouble(Double someDouble)

Here you can find the source of formatDouble(Double someDouble)

Description

Formats a double as a string with a default number of significant digits depending on the argument's value.

License

Apache License

Parameter

Parameter Description
someDouble double to render

Return

such a string

Declaration

public static String formatDouble(Double someDouble) 

Method Source Code


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

import java.text.NumberFormat;

public class Main {
    /**//from  w  w w .j  a v a 2s .c o m
     * Formats a double as a string with a default number of significant digits depending on the argument's value. Does not include the thousands separator.
     * 
     * @param someDouble
     *            double to render
     * @return such a string
     */
    public static String formatDouble(Double someDouble) {
        return formatDouble(someDouble, false);
    }

    /**
     * Formats a double as a string with a default number of significant digits depending on the argument's value.
     * 
     * @param someDouble
     *            double to render
     * @param includeThousandsSeparator
     *            Specify if the comma should be included for numbers like 10,234.56
     * @return such a string
     */
    public static String formatDouble(Double someDouble, boolean includeThousandsSeparator) {

        int numberOfDigitsPastTheDecimal = 4;

        // Reduce the number of digits depending on the value of the double
        if (Math.abs(someDouble) >= 100.0) {
            numberOfDigitsPastTheDecimal = 3;
        }
        if (Math.abs(someDouble) >= 1000.0) {
            numberOfDigitsPastTheDecimal = 2;
        }

        return formatDouble(someDouble, numberOfDigitsPastTheDecimal, includeThousandsSeparator);
    }

    /**
     * Formats a double as a string with the specified number of fractional digits. Includes the thousands separator.
     * 
     * @param someDouble
     *            double to render
     * @param fractionCount
     *            Number of Fraction Digits
     * @return such a string
     */
    public static String formatDouble(Double someDouble, int fractionCount) {
        return formatDouble(someDouble, fractionCount, true);
    }

    /**
     * Formats a double as a string with the specified number of fractional digits.
     * 
     * @param someDouble
     *            double to render
     * @param fractionCount
     *            Number of Fraction Digits
     * @param includeThousandsSeparator
     *            Specify if the comma should be included for numbers like 10,234.56
     * 
     * @return such a string
     */
    public static String formatDouble(Double someDouble, int fractionCount, boolean includeThousandsSeparator) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMinimumFractionDigits(fractionCount);
        numberFormat.setMaximumFractionDigits(fractionCount);
        numberFormat.setGroupingUsed(includeThousandsSeparator);
        String result = numberFormat.format(someDouble);
        return result;
    }
}

Related

  1. formatDouble(double number)
  2. formatDouble(double number)
  3. formatDouble(double number, int decimalDigits)
  4. formatDouble(double number, int integerDigits, int fractionDigits)
  5. formatDouble(double orig)
  6. formatDouble(Double v)
  7. formatDouble(double value)
  8. formatDouble(double value)
  9. formatDouble(double value, int precision)