Friday, December 13, 2013

Format currency with DecimalFormat and DecimalFormatSymbols

Example to print formatted currency with DecimalFormat and DecimalFormatSymbol:

package java_decimalformat;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

/**
 * @web java-buddy.blogspot.com
 */
public class Java_DecimalFormat {

    public static void main(String[] args) {
        
        DecimalFormatSymbols decimalFormatSymbols = 
                new DecimalFormatSymbols();
        decimalFormatSymbols.setGroupingSeparator('\'');
        decimalFormatSymbols.setDecimalSeparator('.');

        DecimalFormat decimalFormat = 
                new DecimalFormat("€ #,###.00", decimalFormatSymbols);
        System.out.println(decimalFormat.format(12345.6789));
        decimalFormat = 
                new DecimalFormat("€ #,###.00####", decimalFormatSymbols);
        System.out.println(decimalFormat.format(12345.6789));
        
        decimalFormat = 
                new DecimalFormat("€ #,###.0000", decimalFormatSymbols);
        System.out.println(decimalFormat.format(0.654321));
        decimalFormat = 
                new DecimalFormat("€ #,##0.0000", decimalFormatSymbols);
        System.out.println(decimalFormat.format(0.654321));
    }

}

print formatted currency with DecimalFormat and DecimalFormatSymbol
print formatted currency with DecimalFormat and DecimalFormatSymbol

No comments:

Post a Comment