Java Currency Format formatCurrency(Float f)

Here you can find the source of formatCurrency(Float f)

Description

format Currency

License

Apache License

Declaration

public static String formatCurrency(Float f) 

Method Source Code


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

import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    public static final int FORMAT_CURRENCY = 1;
    public static final int FORMAT_INTEGER = 2;
    public static final int FORMAT_PERCENT = 3;

    public static String formatCurrency(Float f) {
        return formatFloat(f, FORMAT_CURRENCY);

    }/*from  ww w  . ja v  a  2s  .  c  o  m*/

    public static String formatFloat(Float f, int style) {
        String num = new String("0");

        if (f != null)
            num = new String(f.toString());
        else
            return "";

        NumberFormat nf = null;
        switch (style) {
        case FORMAT_INTEGER: {
            nf = NumberFormat.getIntegerInstance(Locale.US);
            num = nf.format(Double.parseDouble(f.toString()));
            break;
        }
        case FORMAT_PERCENT: {
            nf = NumberFormat.getPercentInstance(Locale.US);
            num = nf.format(Double.parseDouble(f.toString()));
            break;
        }
        default: {
            nf = NumberFormat.getCurrencyInstance(Locale.US);
            num = nf.format(Double.parseDouble(f.toString()));
        }
        }

        return num;
    }
}

Related

  1. formatCurrency(double num)
  2. formatCurrency(Double number)
  3. formatCurrency(Double value)
  4. formatCurrency(final Double currencyValue, final Locale locale, final boolean isGroup)
  5. formatCurrency(Float amt)
  6. formatCurrency(int amount)
  7. formatCurrency(Number currencyValue)
  8. formatCurrency(Object value)
  9. formatCurrency(String currency)