Java Money Format format(Object money)

Here you can find the source of format(Object money)

Description

format

License

Apache License

Declaration

public static String format(Object money) 

Method Source Code


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

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {
    private static final String CURRENCY_FORMAT = "#,#00.00#";

    public static String format(Object money) {
        return format(money, CURRENCY_FORMAT);
    }//from   w  ww . ja v  a 2 s .com

    public static String format(Object money, String formatReg) {
        DecimalFormat df = new DecimalFormat(formatReg);
        return df.format(new BigDecimal(_getStrMoney(money)).doubleValue()).toString().replaceAll("^0(\\d).(\\d+)$",
                "$1.$2");
    }

    private static String _getStrMoney(Object money) {
        String strMoney = "0";
        if (money != null) {
            strMoney = money.toString();
            if (strMoney.equals(""))
                strMoney = "0";
        }
        return strMoney;
    }
}

Related

  1. beautifyMoney(long money)
  2. format2Money(double value)
  3. formatBookkeeperMoney(double cents, Locale locale)
  4. formatDoubleToMoneyString(double num)
  5. formatMoney(BigDecimal bd)