Java Currency Format format2CurrencyWithComma(double amt)

Here you can find the source of format2CurrencyWithComma(double amt)

Description

format Currency With Comma

License

Apache License

Parameter

Parameter Description
amt a parameter

Return

The return result will be formatted as ($###,###,###.##)

Declaration

public static String format2CurrencyWithComma(double amt) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**//from   ww w  .  j  a va2  s  .  c  om
     * @param amt     
     * @return    The return result will be formatted as ($###,###,###.##)
     */
    public static String format2CurrencyWithComma(double amt) {
        String formattedAmount = format2DecimalPointMoneyWithComma(amt);

        if (formattedAmount.startsWith("(")) {
            return formattedAmount.replace("(", "($");
        } else {
            return "$" + formattedAmount;
        }
    }

    /**
     * @param amt
     * @return    The return result will be formatted as (###,###,###.##) 
     */
    public static String format2DecimalPointMoneyWithComma(double amt) {
        boolean isNegativ = (amt < 0);

        String tmp = formatDouble(Math.abs(amt));
        if (isNegativ)
            return "(" + tmp + ")";
        else
            return tmp;
    }

    public static String formatDouble(double value) {
        NumberFormat formatter = new DecimalFormat("#,###,##0.00;(#,###,##0.00)");
        return formatter.format(value);
    }
}

Related

  1. currency(double amount, String language, String country)
  2. currency(double amount, String language, String country)
  3. currency(double value, NumberFormat nformat)
  4. currencyFormat()
  5. formatAsCurrency(BigDecimal nAmount)
  6. formatAsCurrency(final double value)
  7. formatAsCurrency(Number amount)
  8. formatAsCurrency(Number value)