It’s very common task in Java to format double to 2 decimal places – usually to format currency. It’s not easy to do correctly, but in this post we’ll show how.
Java Util Currency Tutorial
The java.util.Currency class represents a currency. These are the main properties of the class:
- Currencies are identified by their ISO 4217 currency codes
- There is never more than one Currency instance for any given currency
- Get instance of the class using getInstance() methods
- the class is final and cannot be inherited.
The class declaration is:
public final class Currency extends Object implements Serializable
Methods inheritance
Objects of this class inherit methods from the following classes:
- java.lang.Object
Methods of java.util.Currency:
- static Set<Currency> getAvailableCurrencies()
This method gets the set of available currencies. - String getCurrencyCode()
This method gets the ISO 4217 currency code of this currency. - int getDefaultFractionDigits()
This method gets the default number of fraction digits used with this currency. - String getDisplayName()
This method gets the name that is suitable for displaying this currency for the default DISPLAY locale. - String getDisplayName(Locale locale)
This method gets the name that is suitable for displaying this currency for the specified locale. - static Currency getInstance(Locale locale)
This method returns the Currency instance for the country of the given locale. - static Currency getInstance(String currencyCode)
This method returns the Currency instance for the given currency code. - int getNumericCode()
This method returns the ISO 4217 numeric code of this currency. - String getSymbol()
This method gets the symbol of this currency for the default DISPLAY locale. - String getSymbol(Locale locale)
This method gets the symbol of this currency for the specified locale. - String toString()
This method returns the ISO 4217 currency code of this currency.
Java Util Currency toString
java.util.Currency.toString()
The method java.util.Currency.toString() overrides Object.toString() and returns the ISO 4217 currency code of this currency.
Java Util Currency getSymbol
java.util.Currency.getSymbol()
The method java.util.Currency.getSymbol() gets the symbol of this currency for the default DISPLAY locale or ISO 4217 currency code.
Java Util Currency getSymbol(Locale)
java.util.Currency.getSymbol(Locale)
The method java.util.Currency.getSymbol(Locale) gets the symbol of this currency for the specified locale or ISO 4217 currency code.
Java Util Currency getNumericCode
java.util.Currency.getNumericCode()
Currencies are identified by their ISO 4217 currency codes. The method java.util.Currency.getNumericCode() returns the ISO 4217 numeric code of this currency.
Java Locale information
Java Locale information
In this tutorial we’re going to show how to get information from Locale, display it nicely to users and get installed locales in the system.
Java Locale create in different ways
Java Locale create in different ways
In this tutorial we’re going to show how to create Locale using builder, constructors, and factory method for different languages and variants to get localized dates.