|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.joda.money.Money
public final class Money
An amount of money with the standard decimal places defined by the currency.
This class represents a quantity of money, stored as a BigDecimal
amount
in a single currency
.
Every currency has a certain standard number of decimal places.
This is typically 2 (Euro, British Pound, US Dollar) but might be
0 (Japanese Yen), 1 (Vietnamese Dong) or 3 (Bahrain Dinar).
The Money
class is fixed to this number of decimal places.
For example, US dollars has a standard number of decimal places of 2. The major units are dollars. The minor units are cents, 100 to the dollar. This class does not allow calculations on fractions of a cent.
This class is immutable and thread-safe.
Method Summary | |
---|---|
Money |
abs()
Returns a copy of this monetary value with a positive amount. |
int |
compareTo(BigMoneyProvider other)
Compares this monetary value to another. |
Money |
convertedTo(CurrencyUnit currency,
BigDecimal conversionMultipler,
RoundingMode roundingMode)
Returns a copy of this monetary value converted into another currency using the specified conversion rate, with a rounding mode used to adjust the decimal places in the result. |
Money |
dividedBy(BigDecimal valueToDivideBy,
RoundingMode roundingMode)
Returns a copy of this monetary value divided by the specified value. |
Money |
dividedBy(double valueToDivideBy,
RoundingMode roundingMode)
Returns a copy of this monetary value divided by the specified value. |
Money |
dividedBy(long valueToDivideBy,
RoundingMode roundingMode)
Returns a copy of this monetary value divided by the specified value. |
boolean |
equals(Object other)
Checks if this monetary value equals another. |
BigDecimal |
getAmount()
Gets the amount. |
BigDecimal |
getAmountMajor()
Gets the amount in major units as a BigDecimal with scale 0. |
int |
getAmountMajorInt()
Gets the amount in major units as an int . |
long |
getAmountMajorLong()
Gets the amount in major units as a long . |
BigDecimal |
getAmountMinor()
Gets the amount in minor units as a BigDecimal with scale 0. |
int |
getAmountMinorInt()
Gets the amount in minor units as an int . |
long |
getAmountMinorLong()
Gets the amount in minor units as a long . |
CurrencyUnit |
getCurrencyUnit()
Gets the currency. |
int |
getMinorPart()
Gets the minor part of the amount. |
int |
getScale()
Gets the scale of the BigDecimal amount. |
int |
hashCode()
Returns a hash code for this monetary value. |
boolean |
isEqual(BigMoneyProvider other)
Checks if this monetary value is equal to another. |
boolean |
isGreaterThan(BigMoneyProvider other)
Checks if this monetary value is greater than another. |
boolean |
isLessThan(BigMoneyProvider other)
Checks if this monetary value is less than another. |
boolean |
isNegative()
Checks if the amount is less than zero. |
boolean |
isNegativeOrZero()
Checks if the amount is zero or less. |
boolean |
isPositive()
Checks if the amount is greater than zero. |
boolean |
isPositiveOrZero()
Checks if the amount is zero or greater. |
boolean |
isSameCurrency(BigMoneyProvider other)
Checks if this instance and the specified instance have the same currency. |
boolean |
isZero()
Checks if the amount is zero. |
Money |
minus(BigDecimal amountToSubtract)
Returns a copy of this monetary value with the amount subtracted. |
Money |
minus(BigDecimal amountToSubtract,
RoundingMode roundingMode)
Returns a copy of this monetary value with the amount subtracted. |
Money |
minus(double amountToSubtract)
Returns a copy of this monetary value with the amount subtracted. |
Money |
minus(double amountToSubtract,
RoundingMode roundingMode)
Returns a copy of this monetary value with the amount subtracted. |
Money |
minus(Iterable<Money> moniesToSubtract)
Returns a copy of this monetary value with a collection of monetary amounts subtracted. |
Money |
minus(Money moneyToSubtract)
Returns a copy of this monetary value with the amount subtracted. |
Money |
minusMajor(long amountToSubtract)
Returns a copy of this monetary value with the amount in major units subtracted. |
Money |
minusMinor(long amountToSubtract)
Returns a copy of this monetary value with the amount in minor units subtracted. |
Money |
multipliedBy(BigDecimal valueToMultiplyBy,
RoundingMode roundingMode)
Returns a copy of this monetary value multiplied by the specified value. |
Money |
multipliedBy(double valueToMultiplyBy,
RoundingMode roundingMode)
Returns a copy of this monetary value multiplied by the specified value. |
Money |
multipliedBy(long valueToMultiplyBy)
Returns a copy of this monetary value multiplied by the specified value. |
Money |
negated()
Returns a copy of this monetary value with the amount negated. |
static Money |
nonNull(Money money,
CurrencyUnit currency)
Ensures that a Money is not null . |
static Money |
of(BigMoneyProvider moneyProvider)
Obtains an instance of Money from a provider. |
static Money |
of(BigMoneyProvider moneyProvider,
RoundingMode roundingMode)
Obtains an instance of Money from a provider, rounding as necessary. |
static Money |
of(CurrencyUnit currency,
BigDecimal amount)
Obtains an instance of Money from a BigDecimal . |
static Money |
of(CurrencyUnit currency,
BigDecimal amount,
RoundingMode roundingMode)
Obtains an instance of Money from a BigDecimal , rounding as necessary. |
static Money |
of(CurrencyUnit currency,
double amount)
Obtains an instance of Money from a double using a
well-defined conversion. |
static Money |
of(CurrencyUnit currency,
double amount,
RoundingMode roundingMode)
Obtains an instance of Money from a double using a
well-defined conversion, rounding as necessary. |
static Money |
ofMajor(CurrencyUnit currency,
long amountMajor)
Obtains an instance of Money from an amount in major units. |
static Money |
ofMinor(CurrencyUnit currency,
long amountMinor)
Obtains an instance of Money from an amount in minor units. |
static Money |
parse(String moneyStr)
Parses an instance of Money from a string. |
Money |
plus(BigDecimal amountToAdd)
Returns a copy of this monetary value with the amount added. |
Money |
plus(BigDecimal amountToAdd,
RoundingMode roundingMode)
Returns a copy of this monetary value with the amount added. |
Money |
plus(double amountToAdd)
Returns a copy of this monetary value with the amount added. |
Money |
plus(double amountToAdd,
RoundingMode roundingMode)
Returns a copy of this monetary value with the amount added. |
Money |
plus(Iterable<Money> moniesToAdd)
Returns a copy of this monetary value with a collection of monetary amounts added. |
Money |
plus(Money moneyToAdd)
Returns a copy of this monetary value with the amount added. |
Money |
plusMajor(long amountToAdd)
Returns a copy of this monetary value with the amount in major units added. |
Money |
plusMinor(long amountToAdd)
Returns a copy of this monetary value with the amount in minor units added. |
Money |
rounded(int scale,
RoundingMode roundingMode)
Returns a copy of this monetary value rounded to the specified scale without changing the current scale. |
BigMoney |
toBigMoney()
Implements the BigMoneyProvider interface, returning a
BigMoney instance with the same currency, amount and scale. |
String |
toString()
Gets the monetary value as a string. |
static Money |
total(CurrencyUnit currency,
Iterable<Money> monies)
Obtains an instance of Money as the total value of
a possibly empty collection. |
static Money |
total(CurrencyUnit currency,
Money... monies)
Obtains an instance of Money as the total value of
a possibly empty array. |
static Money |
total(Iterable<Money> monies)
Obtains an instance of Money as the total value of a collection. |
static Money |
total(Money... monies)
Obtains an instance of Money as the total value of an array. |
Money |
withAmount(BigDecimal amount)
Returns a copy of this monetary value with the specified amount. |
Money |
withAmount(BigDecimal amount,
RoundingMode roundingMode)
Returns a copy of this monetary value with the specified amount. |
Money |
withAmount(double amount)
Returns a copy of this monetary value with the specified amount using a well-defined conversion from a double . |
Money |
withAmount(double amount,
RoundingMode roundingMode)
Returns a copy of this monetary value with the specified amount using a well-defined conversion from a double . |
Money |
withCurrencyUnit(CurrencyUnit currency)
Returns a copy of this monetary value with the specified currency. |
Money |
withCurrencyUnit(CurrencyUnit currency,
RoundingMode roundingMode)
Returns a copy of this monetary value with the specified currency. |
static Money |
zero(CurrencyUnit currency)
Obtains an instance of Money representing zero. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public static Money of(CurrencyUnit currency, BigDecimal amount)
Money
from a BigDecimal
.
This allows you to create an instance with a specific currency and amount. No rounding is performed on the amount, so it must have a scale compatible with the currency.
currency
- the currency, not nullamount
- the amount of money, not null
ArithmeticException
- if the scale exceeds the currency scalepublic static Money of(CurrencyUnit currency, BigDecimal amount, RoundingMode roundingMode)
Money
from a BigDecimal
, rounding as necessary.
This allows you to create an instance with a specific currency and amount. If the amount has a scale in excess of the scale of the currency then the excess fractional digits are rounded using the rounding mode.
currency
- the currency, not nullamount
- the amount of money, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if the rounding failspublic static Money of(CurrencyUnit currency, double amount)
Money
from a double
using a
well-defined conversion.
This allows you to create an instance with a specific currency and amount. No rounding is performed on the amount, so it must have a scale compatible with the currency.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
currency
- the currency, not nullamount
- the amount of money, not null
ArithmeticException
- if the scale exceeds the currency scalepublic static Money of(CurrencyUnit currency, double amount, RoundingMode roundingMode)
Money
from a double
using a
well-defined conversion, rounding as necessary.
This allows you to create an instance with a specific currency and amount. If the amount has a scale in excess of the scale of the currency then the excess fractional digits are rounded using the rounding mode.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
currency
- the currency, not nullamount
- the amount of money, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if the rounding failspublic static Money ofMajor(CurrencyUnit currency, long amountMajor)
Money
from an amount in major units.
This allows you to create an instance with a specific currency and amount.
The amount is a whole number only. Thus you can initialise the value
'USD 20', but not the value 'USD 20.32'.
For example, ofMajor(USD, 25)
creates the instance USD 25.00
.
currency
- the currency, not nullamountMajor
- the amount of money in the major division of the currency
public static Money ofMinor(CurrencyUnit currency, long amountMinor)
Money
from an amount in minor units.
This allows you to create an instance with a specific currency and amount
expressed in terms of the minor unit.
For example, if constructing US Dollars, the input to this method represents cents.
Note that when a currency has zero decimal places, the major and minor units are the same.
For example, ofMajor(USD, 2595)
creates the instance USD 25.95
.
currency
- the currency, not nullamountMinor
- the amount of money in the minor division of the currency
public static Money zero(CurrencyUnit currency)
Money
representing zero.
For example, zero(USD)
creates the instance USD 0.00
.
currency
- the currency, not null
public static Money of(BigMoneyProvider moneyProvider)
Money
from a provider.
This allows you to create an instance from any class that implements the
provider, such as BigMoney
.
No rounding is performed on the amount, so it must have a scale compatible
with the currency.
moneyProvider
- the money to convert, not null
ArithmeticException
- if the scale exceeds the currency scalepublic static Money of(BigMoneyProvider moneyProvider, RoundingMode roundingMode)
Money
from a provider, rounding as necessary.
This allows you to create an instance from any class that implements the
provider, such as BigMoney
.
The rounding mode is used to adjust the scale to the scale of the currency.
moneyProvider
- the money to convert, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if the rounding failspublic static Money total(Money... monies)
Money
as the total value of an array.
The array must contain at least one monetary value.
Subsequent amounts are added as though using plus(Money)
.
All amounts must be in the same currency.
monies
- the monetary values to total, not empty, no null elements, not null
IllegalArgumentException
- if the array is empty
CurrencyMismatchException
- if the currencies differpublic static Money total(Iterable<Money> monies)
Money
as the total value of a collection.
The iterable must provide at least one monetary value.
Subsequent amounts are added as though using plus(Money)
.
All amounts must be in the same currency.
monies
- the monetary values to total, not empty, no null elements, not null
IllegalArgumentException
- if the iterable is empty
CurrencyMismatchException
- if the currencies differpublic static Money total(CurrencyUnit currency, Money... monies)
Money
as the total value of
a possibly empty array.
The amounts are added as though using plus(Money)
starting
from zero in the specified currency.
All amounts must be in the same currency.
currency
- the currency to total in, not nullmonies
- the monetary values to total, no null elements, not null
CurrencyMismatchException
- if the currencies differpublic static Money total(CurrencyUnit currency, Iterable<Money> monies)
Money
as the total value of
a possibly empty collection.
The amounts are added as though using plus(Money)
starting
from zero in the specified currency.
All amounts must be in the same currency.
currency
- the currency to total in, not nullmonies
- the monetary values to total, no null elements, not null
CurrencyMismatchException
- if the currencies differpublic static Money parse(String moneyStr)
Money
from a string.
The string format is '
For example,
[+-]?[0-9]*[.]?[0-9]*
.
This matches the output from toString()
.
of("USD 25")
creates the instance USD 25.00
while of("USD 25.95")
creates the instance USD 25.95
.
moneyStr
- the money string to parse, not null
IllegalArgumentException
- if the string is malformed
ArithmeticException
- if the amount is too large
public static Money nonNull(Money money, CurrencyUnit currency)
Money
is not null
.
If the input money is not null
, then it is returned, providing
that the currency matches the specified currency.
If the input money is null
, then zero money in the currency is returned.
money
- the monetary value to check, may be nullcurrency
- the currency to use, not null
CurrencyMismatchException
- if the input money is non-null and the currencies differpublic CurrencyUnit getCurrencyUnit()
public Money withCurrencyUnit(CurrencyUnit currency)
The returned instance will have the specified currency and the amount from this instance. If the scale differs between the currencies such that rounding would be required, then an exception is thrown.
This instance is immutable and unaffected by this method.
currency
- the currency to use, not null
ArithmeticException
- if the scale of the new currency is less than
the scale of this currencypublic Money withCurrencyUnit(CurrencyUnit currency, RoundingMode roundingMode)
The returned instance will have the specified currency and the amount from this instance. If the number of decimal places differs between the currencies, then the amount may be rounded.
This instance is immutable and unaffected by this method.
currency
- the currency to use, not nullroundingMode
- the rounding mode to use to bring the decimal places back in line, not null
ArithmeticException
- if the rounding failspublic int getScale()
BigDecimal
amount.
The scale has the same meaning as in BigDecimal
.
Positive values represent the number of decimal places in use.
For example, a scale of 2 means that the money will have two decimal places
such as 'USD 43.25'.
For Money
, the scale is fixed and always matches that of the currency.
public BigDecimal getAmount()
This returns the value of the money as a BigDecimal
.
The scale will be the scale of this money.
public BigDecimal getAmountMajor()
BigDecimal
with scale 0.
This returns the monetary amount in terms of the major units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
This is returned as a BigDecimal
rather than a BigInteger
.
This is to allow further calculations to be performed on the result.
Should you need a BigInteger
, simply call BigDecimal.toBigInteger()
.
public long getAmountMajorLong()
long
.
This returns the monetary amount in terms of the major units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
ArithmeticException
- if the amount is too large for a long
public int getAmountMajorInt()
int
.
This returns the monetary amount in terms of the major units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 2, and 'BHD -1.345' will return -1.
ArithmeticException
- if the amount is too large for an int
public BigDecimal getAmountMinor()
BigDecimal
with scale 0.
This returns the monetary amount in terms of the minor units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 235, and 'BHD -1.345' will return -1345.
This is returned as a BigDecimal
rather than a BigInteger
.
This is to allow further calculations to be performed on the result.
Should you need a BigInteger
, simply call BigDecimal.toBigInteger()
.
public long getAmountMinorLong()
long
.
This returns the monetary amount in terms of the minor units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 235, and 'BHD -1.345' will return -1345.
ArithmeticException
- if the amount is too large for a long
public int getAmountMinorInt()
int
.
This returns the monetary amount in terms of the minor units of the currency, truncating the amount if necessary. For example, 'EUR 2.35' will return 235, and 'BHD -1.345' will return -1345.
ArithmeticException
- if the amount is too large for an int
public int getMinorPart()
This return the minor unit part of the monetary amount. This is defined as the amount in minor units excluding major units.
For example, EUR has a scale of 2, so the minor part is always between 0 and 99 for positive amounts, and 0 and -99 for negative amounts. Thus 'EUR 2.35' will return 35, and 'EUR -1.34' will return -34.
public boolean isZero()
public boolean isPositive()
public boolean isPositiveOrZero()
public boolean isNegative()
public boolean isNegativeOrZero()
public Money withAmount(BigDecimal amount)
The returned instance will have this currency and the new amount. No rounding is performed on the amount to be added, so it must have a scale compatible with the currency.
This instance is immutable and unaffected by this method.
amount
- the monetary amount to set in the returned instance, not null
ArithmeticException
- if the scale of the amount is too largepublic Money withAmount(BigDecimal amount, RoundingMode roundingMode)
The returned instance will have this currency and the new amount.
If the scale of the BigDecimal
needs to be adjusted, then
it will be rounded using the specified mode.
This instance is immutable and unaffected by this method.
amount
- the monetary amount to set in the returned instance, not nullroundingMode
- the rounding mode to adjust the scale, not null
public Money withAmount(double amount)
double
.
The returned instance will have this currency and the new amount. No rounding is performed on the amount to be added, so it must have a scale compatible with the currency.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amount
- the monetary amount to set in the returned instance, not null
ArithmeticException
- if the scale of the amount is too largepublic Money withAmount(double amount, RoundingMode roundingMode)
double
.
The returned instance will have this currency and the new amount.
If the scale of the BigDecimal
needs to be adjusted, then
it will be rounded using the specified mode.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amount
- the monetary amount to set in the returned instance, not nullroundingMode
- the rounding mode to adjust the scale, not null
public Money plus(Iterable<Money> moniesToAdd)
This adds the specified amounts to this monetary amount, returning a new object. The amounts must be in the same currency.
This instance is immutable and unaffected by this method.
moniesToAdd
- the monetary values to add, no null elements, not null
CurrencyMismatchException
- if the currencies differpublic Money plus(Money moneyToAdd)
This adds the specified amount to this monetary amount, returning a new object. The amount added must be in the same currency.
The addition has no rounding issues and is always accurate. For example,'USD 25.95' plus 'USD 3.02' will 'USD 28.97'.
This instance is immutable and unaffected by this method.
moneyToAdd
- the monetary value to add, not null
CurrencyMismatchException
- if the currencies differpublic Money plus(BigDecimal amountToAdd)
This adds the specified amount to this monetary amount, returning a new object. No rounding is performed on the amount to be added, so it must have a scale compatible with the currency.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not null
ArithmeticException
- if the scale of the amount is too largepublic Money plus(BigDecimal amountToAdd, RoundingMode roundingMode)
This adds the specified amount to this monetary amount, returning a new object. If the amount to add exceeds the scale of the currency, then the rounding mode will be used to adjust the result.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not nullroundingMode
- the rounding mode to use, not null
public Money plus(double amountToAdd)
This adds the specified amount to this monetary amount, returning a new object. No rounding is performed on the amount to be added, so it must have a scale compatible with the currency.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not null
ArithmeticException
- if the scale of the amount is too largepublic Money plus(double amountToAdd, RoundingMode roundingMode)
This adds the specified amount to this monetary amount, returning a new object. If the amount to add exceeds the scale of the currency, then the rounding mode will be used to adjust the result.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not nullroundingMode
- the rounding mode to use, not null
public Money plusMajor(long amountToAdd)
This adds an amount in major units, leaving the minor units untouched. For example, USD 23.45 plus 138 gives USD 161.45.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not null
public Money plusMinor(long amountToAdd)
This adds an amount in minor units. For example, USD 23.45 plus 138 gives USD 24.83.
This instance is immutable and unaffected by this method.
amountToAdd
- the monetary value to add, not null
public Money minus(Iterable<Money> moniesToSubtract)
This subtracts the specified amounts from this monetary amount, returning a new object. The amounts must be in the same currency.
This instance is immutable and unaffected by this method.
moniesToSubtract
- the monetary values to subtract, no null elements, not null
CurrencyMismatchException
- if the currencies differpublic Money minus(Money moneyToSubtract)
This subtracts the specified amount from this monetary amount, returning a new object. The amount subtracted must be in the same currency.
The subtraction has no rounding issues and is always accurate. For example,'USD 25.95' minus 'USD 3.02' will 'USD 22.93'.
This instance is immutable and unaffected by this method.
moneyToSubtract
- the monetary value to subtract, not null
CurrencyMismatchException
- if the currencies differpublic Money minus(BigDecimal amountToSubtract)
This subtracts the specified amount from this monetary amount, returning a new object. No rounding is performed on the amount to be subtracted, so it must have a scale compatible with the currency.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not null
ArithmeticException
- if the scale of the amount is too largepublic Money minus(BigDecimal amountToSubtract, RoundingMode roundingMode)
This subtracts the specified amount from this monetary amount, returning a new object. If the amount to subtract exceeds the scale of the currency, then the rounding mode will be used to adjust the result.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not nullroundingMode
- the rounding mode to use, not null
public Money minus(double amountToSubtract)
This subtracts the specified amount from this monetary amount, returning a new object. No rounding is performed on the amount to be subtracted, so it must have a scale compatible with the currency.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not null
ArithmeticException
- if the scale of the amount is too largepublic Money minus(double amountToSubtract, RoundingMode roundingMode)
This subtracts the specified amount from this monetary amount, returning a new object. If the amount to subtract exceeds the scale of the currency, then the rounding mode will be used to adjust the result.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not nullroundingMode
- the rounding mode to use, not null
public Money minusMajor(long amountToSubtract)
This subtracts an amount in major units, leaving the minor units untouched. For example, USD 23.45 minus 138 gives USD -114.55.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not null
public Money minusMinor(long amountToSubtract)
This subtracts an amount in minor units. For example, USD 23.45 minus 138 gives USD 22.07.
This instance is immutable and unaffected by this method.
amountToSubtract
- the monetary value to subtract, not null
public Money multipliedBy(BigDecimal valueToMultiplyBy, RoundingMode roundingMode)
This takes this amount and multiplies it by the specified value, rounding the result is rounded as specified.
This instance is immutable and unaffected by this method.
valueToMultiplyBy
- the scalar value to multiply by, not nullroundingMode
- the rounding mode to use to bring the decimal places back in line, not null
ArithmeticException
- if the rounding failspublic Money multipliedBy(double valueToMultiplyBy, RoundingMode roundingMode)
This takes this amount and multiplies it by the specified value, rounding the result is rounded as specified.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
valueToMultiplyBy
- the scalar value to multiply by, not nullroundingMode
- the rounding mode to use to bring the decimal places back in line, not null
ArithmeticException
- if the rounding failspublic Money multipliedBy(long valueToMultiplyBy)
This takes this amount and multiplies it by the specified value.
This instance is immutable and unaffected by this method.
valueToMultiplyBy
- the scalar value to multiply by, not null
public Money dividedBy(BigDecimal valueToDivideBy, RoundingMode roundingMode)
This takes this amount and divides it by the specified value, rounding the result is rounded as specified.
This instance is immutable and unaffected by this method.
valueToDivideBy
- the scalar value to divide by, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if dividing by zero
ArithmeticException
- if the rounding failspublic Money dividedBy(double valueToDivideBy, RoundingMode roundingMode)
This takes this amount and divides it by the specified value, rounding the result is rounded as specified.
The amount is converted via BigDecimal.valueOf(double)
which yields
the most expected answer for most programming scenarios.
Any double
literal in code will be converted to
exactly the same BigDecimal with the same scale.
For example, the literal '1.45d' will be converted to '1.45'.
This instance is immutable and unaffected by this method.
valueToDivideBy
- the scalar value to divide by, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if dividing by zero
ArithmeticException
- if the rounding failspublic Money dividedBy(long valueToDivideBy, RoundingMode roundingMode)
This takes this amount and divides it by the specified value, rounding the result is rounded as specified.
This instance is immutable and unaffected by this method.
valueToDivideBy
- the scalar value to divide by, not nullroundingMode
- the rounding mode to use, not null
ArithmeticException
- if dividing by zero
ArithmeticException
- if the rounding failspublic Money negated()
This instance is immutable and unaffected by this method.
public Money abs()
This instance is immutable and unaffected by this method.
public Money rounded(int scale, RoundingMode roundingMode)
Scale has the same meaning as in BigDecimal
.
A scale of 2 means round to 2 decimal places.
This instance is immutable and unaffected by this method.
scale
- the new scaleroundingMode
- the rounding mode to use, not null
ArithmeticException
- if the rounding failspublic Money convertedTo(CurrencyUnit currency, BigDecimal conversionMultipler, RoundingMode roundingMode)
This instance is immutable and unaffected by this method.
currency
- the new currency, not nullconversionMultipler
- the conversion factor between the currencies, not nullroundingMode
- the rounding mode to use to bring the decimal places back in line, not null
IllegalArgumentException
- if the currency is the same as this currency
IllegalArgumentException
- if the conversion multiplier is negative
ArithmeticException
- if the rounding failspublic BigMoney toBigMoney()
BigMoneyProvider
interface, returning a
BigMoney
instance with the same currency, amount and scale.
toBigMoney
in interface BigMoneyProvider
public boolean isSameCurrency(BigMoneyProvider other)
other
- the money to check, not null
public int compareTo(BigMoneyProvider other)
This allows Money
to be compared to any BigMoneyProvider
.
Scale is ignored in the comparison.
The compared values must be in the same currency.
compareTo
in interface Comparable<BigMoneyProvider>
other
- the other monetary value, not null
CurrencyMismatchException
- if the currencies differpublic boolean isEqual(BigMoneyProvider other)
This allows Money
to be compared to any BigMoneyProvider
.
Scale is ignored, so 'USD 30.00' and 'USD 30' are equal.
The compared values must be in the same currency.
other
- the other monetary value, not null
CurrencyMismatchException
- if the currencies differequals(Object)
public boolean isGreaterThan(BigMoneyProvider other)
This allows Money
to be compared to any BigMoneyProvider
.
Scale is ignored in the comparison.
The compared values must be in the same currency.
other
- the other monetary value, not null
CurrencyMismatchException
- if the currencies differpublic boolean isLessThan(BigMoneyProvider other)
This allows Money
to be compared to any BigMoneyProvider
.
Scale is ignored in the comparison.
The compared values must be in the same currency.
other
- the other monetary value, not null
CurrencyMismatchException
- if the currencies differpublic boolean equals(Object other)
The comparison takes into account the scale. The compared values must be in the same currency.
equals
in class Object
other
- the other object to compare to, not null
public int hashCode()
hashCode
in class Object
public String toString()
The format is the 3 letter ISO currency code, followed by a space,
followed by the amount as per BigDecimal.toPlainString()
.
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |