format money Amount via BigDecimal - Java java.math

Java examples for java.math:BigDecimal Convert

Description

format money Amount via BigDecimal

Demo Code


//package com.java2s;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Currency;

import java.util.Map;

public class Main {


    private static Map<String, String> map;

    public static String formatAmount(String paramCurrencyCode,
            String amount, int currencySymbolShowMode, String fmtPattern,
            Character decimalSep, Character groupingSep) {

        String currencyCode = getCurrencyCode(paramCurrencyCode);
        Currency curr = Currency.getInstance(currencyCode);
        int fractionDigits = curr.getDefaultFractionDigits();

        String decFmtPattern = String.format(fmtPattern,
                filler(fractionDigits, '0'));

        double diviseur = Math.pow(10, fractionDigits);
        BigDecimal dividende = new BigDecimal(amount);
        BigDecimal quotient = dividende.divide(new BigDecimal(diviseur));
        BigDecimal scaled = quotient.setScale(fractionDigits);

        String currencyInfo;//from  w  w w.j av  a 2 s  . c om

        switch (currencySymbolShowMode) {
        case 0:
            currencyInfo = "";
            break;
        case 1:
            currencyInfo = curr.getCurrencyCode();
            break;
        case 2:
            currencyInfo = curr.getSymbol();
            break;
        default:
            currencyInfo = null;
            break;
        }

        curr.getSymbol();

        String formattedAmount = formatDecimalAmount(scaled.doubleValue(),
                currencyCode, decimalSep, groupingSep, currencyInfo,
                decFmtPattern);
        return formattedAmount;
    }

    public static String getCurrencyCode(String paramCurrencyCode) {
        String currencyCode;

        try {
            Integer.parseInt(paramCurrencyCode);
            currencyCode = map.get(paramCurrencyCode);
        } catch (NumberFormatException e) {
            currencyCode = paramCurrencyCode;
        }

        return currencyCode;
    }

    public static String filler(int fillingLength, char fillingChar) {
        char[] buf = new char[fillingLength];

        Arrays.fill(buf, 0, buf.length, fillingChar);

        String res = new String(buf);

        return res;
    }

    public static String formatDecimalAmount(double amount,
            String currencyCode, Character decimalSep,
            Character groupingSep, String currencySymbol,
            String formatPattern) {
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();

        if (null != currencySymbol) {
            dfs.setCurrencySymbol(currencySymbol);
        }

        if (null != decimalSep) {
            dfs.setDecimalSeparator(decimalSep);
        }

        if (null != groupingSep) {
            dfs.setGroupingSeparator(groupingSep);
        }

        DecimalFormat decf = new DecimalFormat(formatPattern, dfs);

        return decf.format(amount);
    }
}

Related Tutorials