format Currency via BigDecimal - Java java.math

Java examples for java.math:BigDecimal Format

Description

format Currency via BigDecimal

Demo Code


//package com.java2s;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal cash = new BigDecimal("1234");
        System.out.println(formatCurrency(cash));
    }//from ww w . j a v a 2 s.c  om

    public static String formatCurrency(BigDecimal cash) {
        DecimalFormat df = (DecimalFormat) DecimalFormat
                .getNumberInstance();
        df.setMinimumIntegerDigits(1);
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        return df.format(cash);
    }
}

Related Tutorials