Here you can find the source of format(double no, String formatter)
Parameter | Description |
---|---|
no | a double number. |
formatter | number formatter string. |
public static String format(double no, String formatter)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; import java.text.DecimalFormat; public class Main { public static String format(BigDecimal no, String formatter) { DecimalFormat df = new DecimalFormat(formatter); return df.format(no); }//from ww w . j a v a 2 s.c om /** * Formats a number to the specific formatter stirng. * * @param no * a number string. * @param formatter * number formatter string. * @return formatted number string. * @exception NumberFormatException * if the no argument is not a number. */ public static String format(String no, String formatter) { return format(Double.parseDouble(no), formatter); } /** * Formats a number to the specific formatter stirng. * * @param no * a int number. * @param formatter * number formatter string. * @return formatted number string. */ public static String format(int no, String formatter) { return format((long) no, formatter); } /** * Formats a number to the specific formatter stirng. * * @param no * a float number. * @param formatter * number formatter string. * @return formatted number string. */ public static String format(float no, String formatter) { return format((double) no, formatter); } /** * Formats a number to the specific formatter stirng. * * @param no * a long number. * @param formatter * number formatter string. * @return formatted number string. */ public static String format(long no, String formatter) { DecimalFormat df = new DecimalFormat(formatter); return df.format(no); } /** * Formats a number to the specific formatter stirng. * * @param no * a double number. * @param formatter * number formatter string. * @return formatted number string. */ public static String format(double no, String formatter) { DecimalFormat df = new DecimalFormat(formatter); return df.format(no); } /** * Returns a new <code>double</code> initialized to the value represented * by the specified <code>String</code>. * * If the string argument is null or empty or not a number( * <code>NumberFormatException<code> occur), * return default value argument. * * * @param value a string. * @param defaultValue default value. * @return the <code>double</code> represented by the argument. */ public static double parseDouble(String value, double defaultValue) { try { return (value == null || value.equals("")) ? defaultValue : Double.parseDouble(value); } catch (NumberFormatException e) { return defaultValue; } } /** * Returns a new <code>double</code> initialized to the value represented * by the specified <code>String</code>. * * If the string argument is null or empty or not a number( * <code>NumberFormatException<code> occur), * return -1.0. * * * @param value a string. * @return the <code>double</code> represented by the argument. */ public static double parseDouble(String value) { return parseDouble(value, -1.0); } }