Java Money Format formatMoney(double money, int scalar)

Here you can find the source of formatMoney(double money, int scalar)

Description

format Money

License

Open Source License

Declaration

public static String formatMoney(double money, int scalar) throws Exception 

Method Source Code

//package com.java2s;
/**//from   w w  w. ja  va 2 s  .  com
 * Copyright (C) 2002-2005 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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

public class Main {

    public static String formatMoney(double money, int scalar) throws Exception {
        String zero = "000000000000000000000000000000";
        String format = "###,##0." + zero.substring(0, scalar);
        NumberFormat nf = new DecimalFormat(format);
        return nf.format(money);
    }

    public static String formatMoney(float money, int scalar) throws Exception {
        String zero = "000000000000000000000000000000";
        String format = "###,##0." + zero.substring(0, scalar);
        NumberFormat nf = new DecimalFormat(format);
        return nf.format(money);
    }

    public static String formatMoney(String money, int scalar, boolean isround, int standard) throws Exception {
        String formatMoney = null;
        if (isround)
            formatMoney = round(money, scalar, standard);
        String zero = "000000000000000000000000000000";
        String format = "###,##0." + zero.substring(0, scalar);
        java.text.NumberFormat nf = new java.text.DecimalFormat(format);
        return (nf.format(formatMoney));
    }

    public static String formatMoney(double money, int scalar, boolean isround, int standard) throws Exception {
        String formatMoney = null;
        if (isround)
            formatMoney = round(String.valueOf(money), scalar, standard);
        String zero = "000000000000000000000000000000";
        String format = "###,##0." + zero.substring(0, scalar);
        java.text.NumberFormat nf = new java.text.DecimalFormat(format);
        return (nf.format(formatMoney));
    }

    public static String formatMoney(String money, int scalar, boolean isround) throws Exception {
        return formatMoney(money, scalar, isround, 5);
    }

    public static String formatMoney(double money, int scalar, boolean isround) throws Exception {
        return formatMoney(String.valueOf(money), scalar, isround, 5);
    }

    public static double round(double value, int scalar, int standard) throws Exception {
        return Double.parseDouble(round(String.valueOf(value), scalar, standard));
    }

    public static String round(String value, int scalar, int standard) throws Exception {

        String back = null;
        int point = value.indexOf(".");
        if (point > -1) {
            int len = value.length() - point + 1;
            if (len <= scalar) {
                back = value;
            } else {
                back = value.substring(0, point + scalar + 1);
                String vsTemp = value.substring(point + scalar + 1, point + scalar + 2);
                if (Integer.parseInt(vsTemp) >= standard) {
                    back = String.valueOf(Integer.parseInt(back) + 1);
                }
            }
        } else {
            back = value;
        }
        return back;
    }
}

Related

  1. formatMoney(BigDecimal bd)
  2. formatMoney(BigDecimal money, Locale locale)
  3. formatMoney(BigDecimal value)
  4. formatMoney(double cents, Locale locale)
  5. formatMoney(double dtSource)
  6. formatMoney(int dosh)
  7. formatMoney(Object str)
  8. formatMoney(String money)
  9. formatMoneyNoSymbol(int pennies)