public final class Money extends Object implements MonetaryAmount, Comparable<MonetaryAmount>, Serializable
MonetaryAmount based
on BigDecimal as numeric representation.
As required by MonetaryAmount this class is final, thread-safe,
immutable and serializable.
This class can be configured with an arbitrary MonetaryContext. The
default MonetaryContext used models by default the same settings as
MathContext.DECIMAL64 . This default MonetaryContext can also
be reconfigured by adding a file /javamoney.properties to the
classpath, with the following content:
# Default MathContext for Money #------------------------------- # Custom MonetaryContext, overrides default entries from # org.javamoney.moneta.Money.monetaryContext # RoundingMode hereby is optional (default = HALF_EVEN) org.javamoney.moneta.Money.defaults.precision=256 org.javamoney.moneta.Money.defaults.roundingMode=HALF_EVEN
| Modifier and Type | Field and Description |
|---|---|
static MonetaryContext |
DEFAULT_MONETARY_CONTEXT
The default
MonetaryContext applied, if not set explicitly on
creation. |
| Modifier and Type | Method and Description |
|---|---|
Money |
abs() |
Money |
add(MonetaryAmount amount) |
static void |
checkNoInfinityOrNaN(Number number)
Deprecated.
Will be removed.
|
int |
compareTo(MonetaryAmount o) |
Money |
divide(double divisor) |
Money |
divide(long divisor) |
Money |
divide(Number divisor) |
Money[] |
divideAndRemainder(double divisor) |
Money[] |
divideAndRemainder(long divisor) |
Money[] |
divideAndRemainder(Number divisor) |
Money |
divideToIntegralValue(double divisor) |
Money |
divideToIntegralValue(long divisor) |
Money |
divideToIntegralValue(Number divisor) |
boolean |
equals(Object obj) |
static Money |
from(MonetaryAmount amt)
Converts (if necessary) the given
MonetaryAmount to a
Money instance. |
MonetaryContext |
getContext()
Access the
MonetaryContext used by this instance. |
CurrencyUnit |
getCurrency()
Returns the amount’s currency, modelled as
CurrencyUnit. |
MonetaryAmountFactory<Money> |
getFactory() |
NumberValue |
getNumber()
Gets the number representation of the numeric value of this item.
|
BigDecimal |
getNumberStripped()
Method that returns BigDecimal.ZERO, if
isZero(), and
#stripTrailingZeros() in all other cases. |
int |
hashCode() |
boolean |
isEqualTo(MonetaryAmount amount) |
boolean |
isGreaterThan(MonetaryAmount amount) |
boolean |
isGreaterThanOrEqualTo(MonetaryAmount amount) |
static boolean |
isInfinityAndNotNaN(Number number)
Deprecated.
Will be removed.
|
boolean |
isLessThan(MonetaryAmount amount) |
boolean |
isLessThanOrEqualTo(MonetaryAmount amount) |
boolean |
isNegative() |
boolean |
isNegativeOrZero() |
boolean |
isPositive() |
boolean |
isPositiveOrZero() |
boolean |
isZero() |
Money |
multiply(double multiplicand) |
Money |
multiply(long multiplicand) |
Money |
multiply(Number multiplicand) |
Money |
negate() |
static Money |
of(BigDecimal number,
CurrencyUnit currency)
Creates a new instance of
Money, using the default
MonetaryContext. |
static Money |
of(BigDecimal number,
CurrencyUnit currency,
MonetaryContext monetaryContext)
Creates a new instance of
Money, using an explicit
MonetaryContext. |
static Money |
of(BigDecimal number,
String currencyCode)
Static factory method for creating a new instance of
Money. |
static Money |
of(BigDecimal number,
String currencyCode,
MonetaryContext monetaryContext)
Static factory method for creating a new instance of
Money. |
static Money |
of(Number number,
CurrencyUnit currency)
Creates a new instance of
Money, using the default
MonetaryContext. |
static Money |
of(Number number,
CurrencyUnit currency,
MonetaryContext monetaryContext)
Creates a new instance of
Money, using an explicit
MonetaryContext. |
static Money |
of(Number number,
String currencyCode)
Static factory method for creating a new instance of
Money. |
static Money |
of(Number number,
String currencyCode,
MonetaryContext monetaryContext)
Static factory method for creating a new instance of
Money. |
static Money |
ofMinor(CurrencyUnit currency,
long amountMinor)
Obtains an instance of
Money from an amount in minor units. |
static Money |
ofMinor(CurrencyUnit currency,
long amountMinor,
int factionDigits)
Obtains an instance of
Money from an amount in minor units. |
static Money |
parse(CharSequence text)
Obtains an instance of Money from a text string such as 'EUR 25.25'.
|
static Money |
parse(CharSequence text,
MonetaryAmountFormat formatter)
Obtains an instance of Money from a text using specific formatter.
|
Money |
plus() |
<R> R |
query(MonetaryQuery<R> query) |
Money |
remainder(double divisor) |
Money |
remainder(long divisor) |
Money |
remainder(Number divisor) |
Money |
scaleByPowerOfTen(int power) |
int |
signum() |
Money |
stripTrailingZeros() |
Money |
subtract(MonetaryAmount subtrahend) |
String |
toString() |
Money |
with(MonetaryOperator operator) |
static Money |
zero(CurrencyUnit currency)
Obtains an instance of
Money representing zero. |
public static final MonetaryContext DEFAULT_MONETARY_CONTEXT
MonetaryContext applied, if not set explicitly on
creation.public CurrencyUnit getCurrency()
CurrencyUnit.
Implementations may co-variantly change the return type to a more
specific implementation of CurrencyUnit if desired.getCurrency in interface CurrencySuppliernullCurrencySupplier.getCurrency()public MonetaryContext getContext()
MonetaryContext used by this instance.getContext in interface MonetaryAmountMonetaryContext used, never null.MonetaryAmount.getContext()public NumberValue getNumber()
getNumber in interface NumberSupplierNumber representation matching best.public BigDecimal getNumberStripped()
isZero(), and
#stripTrailingZeros() in all other cases.public int compareTo(MonetaryAmount o)
compareTo in interface Comparable<MonetaryAmount>public Money abs()
abs in interface MonetaryAmountpublic Money divide(long divisor)
divide in interface MonetaryAmountpublic Money divide(double divisor)
divide in interface MonetaryAmountpublic Money[] divideAndRemainder(long divisor)
divideAndRemainder in interface MonetaryAmountpublic Money[] divideAndRemainder(double divisor)
divideAndRemainder in interface MonetaryAmountpublic Money multiply(long multiplicand)
multiply in interface MonetaryAmountpublic Money multiply(double multiplicand)
multiply in interface MonetaryAmountpublic Money remainder(long divisor)
remainder in interface MonetaryAmountpublic Money remainder(double divisor)
remainder in interface MonetaryAmountpublic boolean isZero()
isZero in interface MonetaryAmountpublic boolean isPositive()
isPositive in interface MonetaryAmountpublic boolean isPositiveOrZero()
isPositiveOrZero in interface MonetaryAmountpublic boolean isNegative()
isNegative in interface MonetaryAmountpublic boolean isNegativeOrZero()
isNegativeOrZero in interface MonetaryAmountpublic <R> R query(MonetaryQuery<R> query)
query in interface MonetaryAmountpublic Money with(MonetaryOperator operator)
with in interface MonetaryAmountpublic Money add(MonetaryAmount amount)
add in interface MonetaryAmountpublic Money divide(Number divisor)
divide in interface MonetaryAmountpublic Money[] divideAndRemainder(Number divisor)
divideAndRemainder in interface MonetaryAmountpublic Money divideToIntegralValue(long divisor)
divideToIntegralValue in interface MonetaryAmountpublic Money divideToIntegralValue(double divisor)
divideToIntegralValue in interface MonetaryAmountpublic Money divideToIntegralValue(Number divisor)
divideToIntegralValue in interface MonetaryAmountpublic Money multiply(Number multiplicand)
multiply in interface MonetaryAmountpublic Money negate()
negate in interface MonetaryAmountpublic Money plus()
plus in interface MonetaryAmountpublic Money subtract(MonetaryAmount subtrahend)
subtract in interface MonetaryAmountpublic Money stripTrailingZeros()
stripTrailingZeros in interface MonetaryAmountpublic Money remainder(Number divisor)
remainder in interface MonetaryAmountpublic Money scaleByPowerOfTen(int power)
scaleByPowerOfTen in interface MonetaryAmountpublic int signum()
signum in interface MonetaryAmountpublic boolean isLessThan(MonetaryAmount amount)
isLessThan in interface MonetaryAmountpublic boolean isLessThanOrEqualTo(MonetaryAmount amount)
isLessThanOrEqualTo in interface MonetaryAmountpublic boolean isGreaterThan(MonetaryAmount amount)
isGreaterThan in interface MonetaryAmountpublic boolean isGreaterThanOrEqualTo(MonetaryAmount amount)
isGreaterThanOrEqualTo in interface MonetaryAmountpublic boolean isEqualTo(MonetaryAmount amount)
isEqualTo in interface MonetaryAmountpublic MonetaryAmountFactory<Money> getFactory()
getFactory in interface MonetaryAmountpublic static Money of(BigDecimal number, CurrencyUnit currency)
Money, using the default
MonetaryContext.number - numeric value, not null.currency - currency unit, not null.Money combining the numeric value and currency unit.ArithmeticException - If the number exceeds the capabilities of the default
MonetaryContext used.public static Money of(BigDecimal number, CurrencyUnit currency, MonetaryContext monetaryContext)
Money, using an explicit
MonetaryContext.number - numeric value, not null.currency - currency unit, not null.monetaryContext - the MonetaryContext to be used, if null the
default MonetaryContext is used.Money instance based on the monetary context with the
given numeric value, currency unit.ArithmeticException - If the number exceeds the capabilities of the
MonetaryContext used.public static Money of(Number number, CurrencyUnit currency)
Money, using the default
MonetaryContext.currency - The target currency, not null.number - The numeric part, not null.Money.ArithmeticException - If the number exceeds the capabilities of the default
MonetaryContext used.public static Money of(Number number, CurrencyUnit currency, MonetaryContext monetaryContext)
Money, using an explicit
MonetaryContext.currency - The target currency, not null.number - The numeric part, not null.monetaryContext - the MonetaryContext to be used, if null the
default MonetaryContext is used.Money.ArithmeticException - If the number exceeds the capabilities of the
MonetaryContext used.public static Money of(Number number, String currencyCode)
Money.currencyCode - The target currency as ISO currency code.number - The numeric part, not null.Money.public static Money of(BigDecimal number, String currencyCode)
Money.currencyCode - The target currency as ISO currency code.number - The numeric part, not null.Money.public static Money of(Number number, String currencyCode, MonetaryContext monetaryContext)
Money.currencyCode - The target currency as ISO currency code.number - The numeric part, not null.monetaryContext - the MonetaryContext to be used, if null the
default MonetaryContext is used.Money.public static Money of(BigDecimal number, String currencyCode, MonetaryContext monetaryContext)
Money.currencyCode - The target currency as ISO currency code.number - The numeric part, not null.monetaryContext - the MonetaryContext to be used, if null the
default MonetaryContext is used.Money.public static Money zero(CurrencyUnit currency)
Money representing zero.currency - the currency, not null.Money representing zero.public static Money ofMinor(CurrencyUnit currency, long amountMinor)
Money from an amount in minor units.
For example, ofMinor(USD, 1234) creates the instance USD 12.34.currency - the currencyamountMinor - the amount of money in the minor division of the currencyNullPointerException - when the currency is nullIllegalArgumentException - when CurrencyUnit.getDefaultFractionDigits() is lesser than zero.CurrencyUnit.getDefaultFractionDigits()public static Money ofMinor(CurrencyUnit currency, long amountMinor, int factionDigits)
Money from an amount in minor units.
For example, ofMinor(USD, 1234, 2) creates the instance USD 12.34.currency - the currency, not nullamountMinor - the amount of money in the minor division of the currencyfactionDigits - number of digitsNullPointerException - when the currency is nullIllegalArgumentException - when the factionDigits is negativeCurrencyUnit.getDefaultFractionDigits(),
ofMinor(CurrencyUnit, long, int)public static Money from(MonetaryAmount amt)
MonetaryAmount to a
Money instance. The MonetaryContext will be adapted as
necessary, if the precision of the given amount exceeds the capabilities
of the default MonetaryContext.amt - the amount to be convertedpublic static Money parse(CharSequence text)
text - the text to parse, not nullNumberFormatException - if the amount is not parseable.UnknownCurrencyException - if the currency is not resolvable.public static Money parse(CharSequence text, MonetaryAmountFormat formatter)
text - the text to parse not nullformatter - the formatter to use not null@Deprecated public static boolean isInfinityAndNotNaN(Number number)
number - the number, not null.@Deprecated public static void checkNoInfinityOrNaN(Number number)
number - the number, not null.Copyright © 2012–2020 JavaMoney. All rights reserved.