Java Currency Format formatCurrency(Double number)

Here you can find the source of formatCurrency(Double number)

Description

Formats double as a string with 2 decimal places

License

Open Source License

Parameter

Parameter Description
number a parameter

Return

currencyString

Declaration

public static String formatCurrency(Double number) 

Method Source Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    /**/*from w  ww. j a v  a 2 s.c  om*/
     * Formats double as a string with 2 decimal places
     * @param number
     * @return currencyString
     */
    public static String formatCurrency(Double number) {
        DecimalFormat formatter = new DecimalFormat("0.00");
        return formatter.format(number);
    }

    /**
     * Formats numeric string as a string with 2 decimal places
     * @param numberStr
     * @return currencyString
     */
    public static String formatCurrency(String numberStr) {
        if (numberStr.matches("\\d+(.\\d+)?")) {
            return formatCurrency(Double.parseDouble(numberStr));
        }
        return "";
    }
}

Related

  1. formatCurrency(BigDecimal bd)
  2. formatCurrency(double amount)
  3. formatCurrency(double amount, int precision, Locale locale)
  4. formatCurrency(double amt)
  5. formatCurrency(double num)
  6. formatCurrency(Double value)
  7. formatCurrency(final Double currencyValue, final Locale locale, final boolean isGroup)
  8. formatCurrency(Float amt)
  9. formatCurrency(Float f)