Class Currency
- All Implemented Interfaces:
Serializable
This class represents a currency Unit
.
Currencies are a special form of DerivedUnit
, conversions
between currencies is possible if their respective exchange rates
have been set and the conversion factor can be changed dynamically.
Quantities stated in Currency
are usually instances of
Money
.
By default, the label associated to a currency is its ISO-4217 code
(see the ISO 4217
maintenance agency for a table of currency codes). An application may
change this default using the UnitFormat.label(String)
method.
For example:[code]
UnitFormat.getStandardInstance().label(Currency.EUR, "€");
UnitFormat.getStandardInstance().label(Currency.GBP, "£");
UnitFormat.getStandardInstance().label(Currency.JPY, "¥");
UnitFormat.getStandardInstance().label(Currency.USD, "$");
[/code]
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Currency
The Australian Dollar currency unit.static final Currency
The Canadian Dollar currency unit.static final Currency
The China Yan currency.static final Currency
The Euro currency.static final Currency
The British Pound currency.static final Currency
The Japanese Yen currency.static final Currency
The Korean Republic Won currency.static final Currency
The Taiwanese dollar currency.static final Currency
The United State dollar currency. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Indicates if the specified unit can be considered equals to the one specified.getCode()
Returns the currency code for this currency.int
Returns the default number of fraction digits used with this currency unit.double
Returns the exchange rate for thisCurrency
.static Currency
Returns the currency used as reference when setting the exchange rate.Returns thebase unit
,alternate unit
or product of base units and alternate units this unit is derived from.int
hashCode()
Returns the hash code for this unit.void
setExchangeRate
(double refAmount) Sets the exchange rate of thisCurrency
relatively to the reference currency.static void
setReferenceCurrency
(Currency currency) Sets the reference currency (context-local).Returns the converter from this unit to its system unit.
-
Field Details
-
AUD
The Australian Dollar currency unit. -
CAD
The Canadian Dollar currency unit. -
CNY
The China Yan currency. -
EUR
The Euro currency. -
GBP
The British Pound currency. -
JPY
The Japanese Yen currency. -
KRW
The Korean Republic Won currency. -
TWD
The Taiwanese dollar currency. -
USD
The United State dollar currency.
-
-
Constructor Details
-
Currency
Creates the currency unit for the given currency code. See the ISO 4217 maintenance agency for more information, including a table of currency codes.- Parameters:
code
- the ISO-4217 code of the currency (e.g."EUR", "USD", "JPY"
).- Throws:
IllegalArgumentException
- if the specified code is not an ISO-4217 code.
-
-
Method Details
-
getCode
Returns the currency code for this currency.- Returns:
- the ISO-4217 code of the currency
(e.g.
"EUR", "USD", "JPY"
).
-
getDefaultFractionDigits
public int getDefaultFractionDigits()Returns the default number of fraction digits used with this currency unit. For example, the default number of fraction digits for theEUR
is 2, while for theJPY
(Yen) it's 0. This method can be overriden for custom currencies returning values different from2
.- Returns:
- the default number of fraction digits for this currency.
-
setReferenceCurrency
Sets the reference currency (context-local). Changing the reference currency clears all the exchange rates previously set.- Parameters:
currency
- the new reference currency.- See Also:
-
LocalContext
-
getReferenceCurrency
Returns the currency used as reference when setting the exchange rate. By default, the reference currency is the currency for the default country locale.- Returns:
- the reference currency.
- See Also:
-
setExchangeRate
public void setExchangeRate(double refAmount) Sets the exchange rate of thisCurrency
relatively to the reference currency. Setting the exchange rate allows for conversion betweenMoney
stated in different currencies. For example:Currency.setReferenceCurrency(Currency.USD); Currency.EUR.setExchangeRate(1.17); // 1.0 € = 1.17 $
- Parameters:
refAmount
- the amount stated in thegetReferenceCurrency()
equals to one unit of thisCurrency
.- See Also:
-
getExchangeRate
public double getExchangeRate()Returns the exchange rate for thisCurrency
.- Returns:
- the amount stated in the
getReferenceCurrency()
equals to one unit of thisCurrency
. - Throws:
ConversionException
- if the exchange rate has not be set for thisCurrency
.
-
equals
Description copied from class:Unit
Indicates if the specified unit can be considered equals to the one specified. -
hashCode
public int hashCode()Description copied from class:Unit
Returns the hash code for this unit. -
getStandardUnit
Description copied from class:Unit
Returns thebase unit
,alternate unit
or product of base units and alternate units this unit is derived from. The standard unit identifies the "type" ofquantity
for which this unit is employed. For example:[code] boolean isAngularVelocity(Unit<?> u) { return u.getStandardUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); [/code]Note: Having the same system unit is not sufficient to ensure that a converter exists between the two units (e.g. °C/m and K/m).
- Specified by:
getStandardUnit
in classUnit<Money>
- Returns:
- the system unit this unit is derived from.
-
toStandardUnit
Description copied from class:Unit
Returns the converter from this unit to its system unit.- Specified by:
toStandardUnit
in classUnit<Money>
- Returns:
this.getConverterTo(this.getSystemUnit())
-