Wednesday, September 24, 2014

Convert Number to Local Currency Using Java

package com;

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

public class NumberToCurrencyEngine
{
   public static void main(String[] args)
   {
      //This is the amount which we want to format
      Double currencyAmount = new Double(50000);
       
      //Get current locale information
      Locale currentLocale = Locale.getDefault();
       
      //Get currency instance from locale; This will have all currency related information
      Currency currentCurrency = Currency.getInstance(currentLocale);
       
      //Currency Formatter specific to locale
      NumberFormat currencyFormatter =      NumberFormat.getCurrencyInstance(currentLocale);

      //Test the output
      System.out.println(currentLocale.getDisplayName());
       
      //System.out.println(currentCurrency.getDisplayName());
       
      System.out.println(currencyFormatter.format(currencyAmount));
   }
}

Output
English (India)

Rs.50,000.00

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...