Java Currency Format currency(double amount, String language, String country)

Here you can find the source of currency(double amount, String language, String country)

Description

Format a currency amount for a given language and country.

License

Apache License

Parameter

Parameter Description
amount the currency value to format
language the two character language code required to construct a <code>java.util.Locale</code>
country the two character country code required to construct a <code>java.util.Locale</code>

Return

a formatted String representing the amount

Declaration

public static String currency(double amount, String language, String country) 

Method Source Code

//package com.java2s;
/*//from  ww w.  j  ava  2 s  .c o m
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    /**
     * Format a currency amount in a given locale.
     * 
      * @param amount the currency value to format
      * @param locale the <code>java.util.Locale</code> to use to format the amount
      * @return a formatted <code>String</code> representing the amount
      */
    public static String currency(double amount, Locale locale) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
        return nf.format(amount);
    }

    /**
     * Format a currency amount for a given language and country.
     * 
      * @param amount the currency value to format
      * @param language the two character language code required to construct a 
      * <code>java.util.Locale</code>
      * @param country the two character country code required to construct a 
      * <code>java.util.Locale</code>
      * @return a formatted <code>String</code> representing the amount
      */
    public static String currency(double amount, String language, String country) {
        Locale locale = getLocale(language, country);
        return currency(amount, locale);
    }

    /**
     * Utility method to guarantee a Locale.
     */
    private static Locale getLocale(String language, String country) {
        Locale locale = null;
        if (language == null || country == null) {
            locale = Locale.getDefault();
        } else {
            locale = new Locale(language, country);
        }
        return locale;
    }
}

Related

  1. currency(double amount, String language, String country)
  2. currency(double value, NumberFormat nformat)
  3. currencyFormat()
  4. format2CurrencyWithComma(double amt)