Java Currency Format formatCurrencyWithCurrencyUnit(double amt, int fractionDigits)

Here you can find the source of formatCurrencyWithCurrencyUnit(double amt, int fractionDigits)

Description

format Currency With Currency Unit

License

Apache License

Declaration

public static String formatCurrencyWithCurrencyUnit(double amt, int fractionDigits) 

Method Source Code

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

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

import java.util.Locale;

public class Main {
    public static String formatCurrencyWithCurrencyUnit(double amt, int fractionDigits) {
        NumberFormat nf = getCurrencyFormat(fractionDigits);
        String currAmt = nf.format(amt);
        String symbol = (nf instanceof DecimalFormat)
                ? ((DecimalFormat) nf).getDecimalFormatSymbols().getCurrencySymbol()
                : "";

        if (symbol.length() == 1) {
            currAmt = currAmt + " " + nf.getCurrency();
        }//from  ww w  .  j a  v  a  2  s  .  c om

        return currAmt;
    }

    public static NumberFormat getCurrencyFormat() {
        return getCurrencyFormat(2);
    }

    public static NumberFormat getCurrencyFormat(int fractionDigits) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

        if (fractionDigits != 2) {
            nf.setMinimumFractionDigits(fractionDigits);
            nf.setMaximumFractionDigits(fractionDigits);
        }

        return nf;
    }
}

Related

  1. formatCurrency(Float f)
  2. formatCurrency(int amount)
  3. formatCurrency(Number currencyValue)
  4. formatCurrency(Object value)
  5. formatCurrency(String currency)
  6. getCurrencyFormat()
  7. getCurrencyFormat(final Locale locale)
  8. getCurrencyFormatter()
  9. parseCurrency(String currency)