Java BigDecimal Format format(BigDecimal no, String formatter)

Here you can find the source of format(BigDecimal no, String formatter)

Description

format

License

Open Source License

Declaration

public static String format(BigDecimal no, String formatter) 

Method Source Code

//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  .co  m

    /**
     * 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);
    }
}

Related

  1. format(BigDecimal amount, String format)
  2. format(BigDecimal decData, int precision, int scale)
  3. format(BigDecimal decimal)
  4. format(BigDecimal n, int prec)
  5. format(BigDecimal n, int prec)
  6. format(BigDecimal num)
  7. format(BigDecimal num)
  8. format(BigDecimal num)
  9. format(BigDecimal number, String format)