Java Fraction Format format(double no, String formatter)

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

Description

Formats a number to the specific formatter stirng.

License

Open Source License

Parameter

Parameter Description
no a double number.
formatter number formatter string.

Return

formatted number string.

Declaration

public static String format(double 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.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);
    }
}

Related

  1. format(Double d, int digit)
  2. format(double d, java.text.NumberFormat format)
  3. format(double degrees)
  4. format(double input, String format)
  5. format(double n)
  6. format(Double num)
  7. format(Double num)
  8. format(double num)
  9. format(double num, int n)