Helper class for format number and currency : Currency « I18N « Java






Helper class for format number and currency

 

/* Copyright 2004 Sun Microsystems, Inc.  All rights reserved.  You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: 
 http://adventurebuilder.dev.java.net/LICENSE.txt
 $Id: I18nUtil.java,v 1.2 2004/05/26 00:07:34 inder Exp $ */

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

/**
 * This utility class for internationalization. This class provides a central
 * location to do specialized formatting in both a default and a locale specfic
 * manner.
 */
public class Main {

  public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);
    return df.format(amount);
  }

  public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);
    return df.format(amount);
  }

  public static String formatCurrency(double amount, int precision, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    nf.setMinimumFractionDigits(precision);
    nf.setMaximumFractionDigits(precision);
    return nf.format(amount);
  }

  public static String formatNumber(double amount, int precision, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setMinimumFractionDigits(precision);
    nf.setMaximumFractionDigits(precision);
    return nf.format(amount);
  }
}

   
  








Related examples in the same category

1.Demonstrate Currency.