Tuesday, 3 November 2015

Currency class in Java

Currency class in Java

public final class Currency implements Serializable

Represents a currency. Currencies are identified by their ISO 4217 currency codes. The class is designed so that there's never more than one Currency instance for any given currency. Therefore, there's no public constructor (use getInstance method to  Currency instance).


import java.util.Currency;
import java.util.*;

public class Currencies {
     public static void main(String args[]) {
           // Locale defaultLocale = Locale.getDefault();
           Locale loc = Locale.CHINA;
           Currency c = Currency.getInstance(loc);

           System.out.println("Currency code :" + c.getCurrencyCode());

           String symbol = c.getSymbol();
           System.out.println("Currency symbol is = " + symbol);

           int usdDigits = c.getDefaultFractionDigits();
           System.out.println("USD fraction digits:" + usdDigits);
     }
}

Output:
     Currency code :CNY
     Currency symbol is = CNY
     USD fraction digits:2

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...