java.lang.Object
com.aoapps.lang.i18n.Money
- All Implemented Interfaces:
FastExternalizable
,Externalizable
,Serializable
,Comparable<Money>
Stores a monetary value as a combination of currency and amount. It supports
many of the basic operators from
BigDecimal
, and more will be added
as needed. An ArithmeticException
on any attempt to perform operations
on monetary values of different currencies.- Author:
- AO Industries, Inc.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMoney()
Deprecated, for removal: This API element is subject to removal in a future version.Only required for implementation, do not use directly.Money
(Currency currency, BigDecimal value) Will change the scale of the value to match the currency, but will not round. -
Method Summary
Modifier and TypeMethodDescriptionint
Sorts by currency code and then value.boolean
Equal when has same currency, value, and scale.int
getScale()
Gets the scale of this currency.long
Gets the serialVersionUID for this object.long
Gets the unscaled value of this currency.getValue()
int
hashCode()
multiply
(BigDecimal multiplicand) Multiplies without rounding.multiply
(BigDecimal multiplicand, RoundingMode roundingMode) Multiplies with rounding.negate()
Returns a monetary amount that is the negative of this amount.static Money
parseMoney
(Currency currency, Locale locale, String value) static BigDecimal
parseMoneyAmount
(Locale locale, String symbol, String value) Parses a monetary amount with an optional symbol prefix.void
toString()
Displays the monetary value as currency symbol (in Locale-specific display) either proceeding or following the value, such as"$100.00"
,"Can$-100.50"
,"100,00 $ CA"
(with non-breaking spaces).void
-
Constructor Details
-
Money
Deprecated, for removal: This API element is subject to removal in a future version.Only required for implementation, do not use directly.- See Also:
-
Money
Will change the scale of the value to match the currency, but will not round.- Throws:
NumberFormatException
- if unable to scale the value.
-
Money
- Throws:
NumberFormatException
-
-
Method Details
-
parseMoneyAmount
public static BigDecimal parseMoneyAmount(Locale locale, String symbol, String value) throws NumberFormatException Parses a monetary amount with an optional symbol prefix. Trims, strips the prefix, trims, removes any commas.TODO: A future version should be more locale-aware regarding assumption comma is used for group separator.
- Parameters:
symbol
- The optional currency prefix to strip (seeCurrencyUtil.getSymbol(java.util.Currency, java.util.Locale)
)value
- The value to parse- Returns:
- The monetary amount or
null
if empty - Throws:
NumberFormatException
- when not a value amount after filtering
-
parseMoney
- See Also:
-
equals
Equal when has same currency, value, and scale. -
hashCode
public int hashCode() -
compareTo
Sorts by currency code and then value.- Specified by:
compareTo
in interfaceComparable<Money>
- See Also:
-
getCurrency
-
getValue
-
getUnscaledValue
public long getUnscaledValue()Gets the unscaled value of this currency. -
getScale
public int getScale()Gets the scale of this currency. -
toString
Displays the monetary value as currency symbol (in Locale-specific display) either proceeding or following the value, such as"$100.00"
,"Can$-100.50"
,"100,00 $ CA"
(with non-breaking spaces). -
add
- Throws:
ArithmeticException
-
multiply
Multiplies without rounding.- Throws:
ArithmeticException
-
multiply
public Money multiply(BigDecimal multiplicand, RoundingMode roundingMode) throws ArithmeticException Multiplies with rounding.- Throws:
ArithmeticException
-
negate
Returns a monetary amount that is the negative of this amount. -
subtract
- Throws:
ArithmeticException
-
getSerialVersionUID
public long getSerialVersionUID()Description copied from interface:FastExternalizable
Gets the serialVersionUID for this object. Since FastExternalizable-aware containers are not special JVM constructs, they do not have access to the private and protected fields. It is assumed that two separate instances of the same class will always return the same serialVersionUID.- Specified by:
getSerialVersionUID
in interfaceFastExternalizable
-
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
ClassNotFoundException
-