Java Locale Format formatDouble(double inVal, int inDecs)

Here you can find the source of formatDouble(double inVal, int inDecs)

Description

Formats a double value to specified number of decimal places runs through the internationalized NumberFormat, so may NOT do scientific notation

License

Open Source License

Parameter

Parameter Description
inVal the input value
inDecs the number of decimals places to be displayed

Return

the string of the double

Declaration

public static String formatDouble(double inVal, int inDecs) 

Method Source Code

//package com.java2s;
// This software is licensed under the GNU General Public License,

import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    private static Locale sLocale;
    private static String INFINITY = Double.toString(Double.POSITIVE_INFINITY);
    private static String NAN = Double.toString(Double.NaN);

    /**/*from   w w w  .  ja va  2 s  .co  m*/
     * Formats a double value to specified number of decimal places runs through the internationalized NumberFormat, so
     * may NOT do scientific notation
     * 
     * @param inVal
     *           the input value
     * @param inDecs
     *           the number of decimals places to be displayed
     * @return the string of the double
     */
    public static String formatDouble(double inVal, int inDecs) {
        return formatDouble(inVal, inDecs, 14);
    }

    /**
     * Formats a double value to specified number of decimal places runs through the internationalized NumberFormat, so
     * may NOT do scientific notation
     * 
     * @param inVal
     *           double number to be formatted
     * @param inDecs
     *           integer of number of decimal places
     * @param inLeftOfDec
     *           integer of max number of places to left of decimal
     * @return the string of the double
     */
    public static String formatDouble(double inVal, int inDecs, int inLeftOfDec) {
        String returnVal = "";
        if (Double.isInfinite(inVal)) {
            returnVal = INFINITY;
        } else if (Double.isNaN(inVal)) {
            returnVal = NAN;
        } else {
            NumberFormat nf = (sLocale == null) ? NumberFormat.getInstance() : NumberFormat.getInstance(sLocale);
            nf.setMaximumIntegerDigits(inLeftOfDec);
            nf.setMinimumFractionDigits(inDecs);
            nf.setGroupingUsed(false);
            returnVal = nf.format(inVal);
        }

        return returnVal;
    }
}

Related

  1. formatDateTime(java.util.Date date, String format, String locale, String timeZone)
  2. formatDateToSQLString(Date srcDate)
  3. formatDecimal(BigInteger b)
  4. formatDecimalDisplay(final double _numberToFormat, final String _formatToApply)
  5. formatDouble(double in)
  6. formatDouble(Double localDouble, int scale)
  7. formatDouble(double number)
  8. formatDouble(double v)
  9. formatDouble(double val, String format)