Package com.highdeal.currency
Class Money
java.lang.Object
com.highdeal.currency.Money
- All Implemented Interfaces:
Serializable
Money is an immutable representing an amount of money with
an arbitrary precision.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new money amount by parsing the stringsthat must be like EUR 0.00 or USD 0.00 for instance.Money(BigDecimal anAmount, Currency aCurrency) Create a new money amount for a currency and with an internal precision sets to the currency default precision.Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision) Create a new money amount for a currency and with an internal precision.Money(BigDecimal anAmount, Currency aCurrency, int internalPrecision, int roundingMode) Create a new money amount for a currency and with an internal precision.Money(BigDecimal anAmount, String currencyCode) Create a new money amount for a currency and with an internal precision sets to the currency default precision.Money(BigDecimal anAmount, String currencyCode, int internalPrecision, int roundingMode) Create a new money amount for a currency and with an internal precision. -
Method Summary
Modifier and TypeMethodDescriptionReturns a money amount whose value is the sum of this and val and whose internal precision is the maximum of the internal precisions of this and val.Returns a money amount whose value is the sum of this and val with a specified precision and rouding mode.intReturns 0 if amounts are equals (same currency and same value), -1, or 1.static intconvertRounding(int rounding) Converts a money rounding into a big decimal rounding.divide(BigDecimal val, int roundingMode) Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode.divide(BigDecimal val, int internalPrecision, int roundingMode) Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode and with a given limited precision.Creates a duplicate of this money amount.booleanReturns true if amounts are equals (same currency and same value), false otherwise.Returns the amount for this money amount.Returns the currency for this money amount.static StringgetRounding(int rounding) Returns the string representation of the rounding mode corresponding to a money rounding mode.static intgetRounding(String rounding) Returns the money rounding from a string representation of the rounding.inthashCode()static booleanTests if the String s is in money format.booleanisZero()Returns true if amount is zero, whatever the currency, false otherwise.Returns the maximum of this and val.Returns the minimum of this and val.multiply(BigDecimal val, int roundingMode) Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode.multiply(BigDecimal val, int internalPrecision, int roundingMode) Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode and with a given limited precision.negate()Returns a money amount whose value is the opposite of this amount.round(int roundingMode) Returns a new money amount whose value is rounded to the precision of its currency using the specified rounding mode.round(int precision, int roundingMode) Returns a new money amount whose value is rounded to the specified precision and rounding mode.Returns a money amount whose value is the difference of this and val and whose internal precision is the maximum of the internal precisions of this and val.Returns a money amount whose value is the difference of this and val with a specified precision and rouding mode.toEMUCurrency(Currency toCurrency) Converts the money amount to a EMU currency.toEuro()Converts the money amount to Euro.toString()Gives a string representation of the amount.
-
Field Details
-
NEAREST
public static final int NEARESTRounds toward nearest neighbor. If the discarded fraction is >= .5, then raise, otherwise truncate.- See Also:
-
TRUNCATE
public static final int TRUNCATETruncate the money amount. The fraction is discarded.- See Also:
-
RAISE
public static final int RAISEIncrements the digit prior to a non-zero discarded fraction.- See Also:
-
-
Constructor Details
-
Money
Creates a new money amount by parsing the stringsthat must be like EUR 0.00 or USD 0.00 for instance.- Parameters:
s- the string to be parsed.- Throws:
IllegalArgumentException- a parsing error occurs
-
Money
Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded to the nearest amount with internal precision.- Parameters:
anAmount- the initial money amount.aCurrency- the currency for this money amount.internalPrecision- the internal precision used for computations.
-
Money
Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded accorded to the with internal precision.- Parameters:
anAmount- the initial money amount.aCurrency- the currency for this money amount.internalPrecision- the internal precision used for computations.roundingMode- the mode of rounding NEAREST, TRUNCATE or RAISE
-
Money
Create a new money amount for a currency and with an internal precision sets to the currency default precision.- Parameters:
anAmount- the initial amount.currencyCode- the currency code for this amount.- See Also:
-
Money
Create a new money amount for a currency and with an internal precision. If the amount has a scale which is greater than internal precision, then it is rounded accorded to the with internal precision.- Parameters:
anAmount- the initial money amount.currencyCode- the currency code for this money amount.internalPrecision- the internal precision used for computations.roundingMode- the mode of rounding NEAREST, TRUNCATE or RAISE
-
Money
Create a new money amount for a currency and with an internal precision sets to the currency default precision.- Parameters:
anAmount- the initial amount.aCurrency- the currency for this amount.- See Also:
-
-
Method Details
-
isZero
public boolean isZero()Returns true if amount is zero, whatever the currency, false otherwise.- Returns:
- true if amount is zero, false otherwise.
-
duplicate
Creates a duplicate of this money amount.- Returns:
- a duplicate of this money amount.
-
getCurrency
Returns the currency for this money amount.- Returns:
- the currency for this money amount.
-
getAmount
Returns the amount for this money amount.- Returns:
- the amount for this money amount.
-
negate
Returns a money amount whose value is the opposite of this amount.- Returns:
- the opposite amount for this money amount.
-
add
Returns a money amount whose value is the sum of this and val and whose internal precision is the maximum of the internal precisions of this and val. Both money amounts must have the same currency.- Parameters:
val- The money to add.- Returns:
- The sum of the money amounts.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not equals.
-
add
Returns a money amount whose value is the sum of this and val with a specified precision and rouding mode. Both money amounts must have the same currency.- Parameters:
val- The money to add.precision- The number of digits for the precision.roundingMode- The rounding mode.- Returns:
- The sum of the money amounts.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not equals.
-
subtract
Returns a money amount whose value is the difference of this and val and whose internal precision is the maximum of the internal precisions of this and val. Both money amounts must have the same currency.- Parameters:
val- The value to subtract.- Returns:
- The sum of the money amounts.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not equals.
-
subtract
public Money subtract(Money val, int precision, int roundingMode) throws IncompatibleCurrenciesException Returns a money amount whose value is the difference of this and val with a specified precision and rouding mode. Both money amounts must have the same currency.- Parameters:
val- The value to subtract.precision- The number of digits for the precision.roundingMode- The rounding mode.- Returns:
- The difference of the money amounts.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not equals.
-
multiply
Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode.- Parameters:
val- The value to multiply by.roundingMode- The rounding mode.- Returns:
- the multiplication of an amount by a constant.
-
multiply
Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode and with a given limited precision.- Parameters:
val- The value to multiply by.internalPrecision- The precision to use for the multiplication.roundingMode- The rounding mode.- Returns:
- the multiplication of an amount by a constant.
-
divide
Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode.- Parameters:
val- The value to divide by.roundingMode- The rounding mode.- Returns:
- the quotient of an amount by a constant.
- Throws:
ArithmeticException- if val is zero.
-
divide
public Money divide(BigDecimal val, int internalPrecision, int roundingMode) throws ArithmeticException Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode and with a given limited precision.- Parameters:
val- The value to divide by.internalPrecision- The precision of the result.roundingMode- The rounding mode.- Returns:
- the quotient of an amount by a constant.
- Throws:
ArithmeticException- if val is zero.
-
max
Returns the maximum of this and val.- Parameters:
val- The value from which the maximum must be chosen.- Returns:
- the quotient of an amount by a constant.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not the same.
-
min
Returns the minimum of this and val.- Parameters:
val- The value from which the minimum must be chosen.- Returns:
- the quotient of an amount by a constant.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not the same.
-
equals
Returns true if amounts are equals (same currency and same value), false otherwise. -
hashCode
public int hashCode() -
compareTo
Returns 0 if amounts are equals (same currency and same value), -1, or 1.- Parameters:
val- The money to compare to.- Returns:
- 0, 1, or -1.
- Throws:
IncompatibleCurrenciesException- if the currencies of this and val are not the same.
-
round
Returns a new money amount whose value is rounded to the precision of its currency using the specified rounding mode.- Parameters:
roundingMode- The rounding mode.- Returns:
- a rounded amount.
-
round
Returns a new money amount whose value is rounded to the specified precision and rounding mode.- Parameters:
precision- the precision.roundingMode- the rounding mode.- Returns:
- a rounded amount.
-
toString
Gives a string representation of the amount. -
toEuro
Converts the money amount to Euro.- Returns:
- the amount in euro.
- Throws:
IncompatibleCurrenciesException- if currency of the money is not a EMU currency.
-
toEMUCurrency
Converts the money amount to a EMU currency.- Parameters:
toCurrency- The target EMU currency.- Returns:
- the converted amount.
- Throws:
IncompatibleCurrenciesException- if the destination currency is not a AMU currency.
-
isMoneyFormat
Tests if the String s is in money format.- Parameters:
s- The string to check.- Returns:
- true or false.
-
convertRounding
public static int convertRounding(int rounding) Converts a money rounding into a big decimal rounding.- Parameters:
rounding- The money rounding.- Returns:
- The corresponding money rounding.
-
getRounding
Returns the money rounding from a string representation of the rounding.- Parameters:
rounding- A string representation of the rounding.- Returns:
- The money rounding mode, or -1 if there is no corresponding rounding mode.
-
getRounding
Returns the string representation of the rounding mode corresponding to a money rounding mode.- Parameters:
rounding- The string representation of the rounding mode.- Returns:
- The string representation of the rounding mode, or null if the string does not match any rounding mode.
-