Search This Blog

Monday, April 5, 2010

How to get different country currency formats in java?

Java provides NumberFormat class to support currency formats based on local. All you need to do is get currency instance for NumberFormat class and calling format() method on top of the instance. An example is given below for your reference.


Sample Code:


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


public class Currency {


    public static void main(String a[]){
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
        double currency = 52.12;
        System.out.println("In Dollars: "+nf.format(currency));
        nf = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.println("In British Pound: "+nf.format(currency));
    }
}


Output:


In Dollars: $52.12
In British Pound: £52.12

No comments:

Post a Comment