Here you can find the source of format(Double decimal)
Parameter | Description |
---|---|
decimal | the decimal, allows null |
public static String format(Double decimal)
//package com.java2s; //License from project: Apache License public class Main { public static final int DEFAULT_PRECISION = 3; /**//from w ww.j a va 2 s . c o m * Attempts to format a Double to a String to the {@link #DEFAULT_PRECISION}. If the * <tt>decimal</tt> is null, null is returned. * @param decimal the decimal, allows null * @return the formatted decimal String or null */ public static String format(Double decimal) { return format(decimal, DEFAULT_PRECISION); } /** * Attempts to format a Double to a String to <tt>toDecimalPlace</tt> decimal places. If the * <tt>decimal</tt> is null, null is returned. * @param decimal the decimal, allows null * @param toDecimalPlace the expected amount of decimals, greater than or equal to 0 * @return the formatted decimal String or null * @throws NumberFormatException when the <tt>toDecimalPlace</tt> is less than 0 */ public static String format(Double decimal, int toDecimalPlace) { return format(decimal, toDecimalPlace, null); } /** * Attempts to format a Double to a String to <tt>toDecimalPlace</tt> decimal places. If the * <tt>decimal</tt> is null, <tt>returnOnNull</tt> is returned. * @param decimal the decimal, allows null * @param toDecimalPlace the expected amount of decimals, greater than or equal to 0 * @param returnOnNull the String value to retunr if the Decimal is null * @return the formatted decimal String or null * @throws NumberFormatException when the <tt>toDecimalPlace</tt> is less than 0 */ public static String format(Double decimal, int toDecimalPlace, Double returnOnNull) { if (toDecimalPlace < 0) { throw new NumberFormatException(); } if (decimal == null) { if (returnOnNull == null) { return null; } decimal = returnOnNull; } return String.format("%1$,." + toDecimalPlace + "f", decimal); } }