Here you can find the source of formatDouble(double inVal, int inDecs)
Parameter | Description |
---|---|
inVal | the input value |
inDecs | the number of decimals places to be displayed |
public static String formatDouble(double inVal, int inDecs)
//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; } }