com.highdeal.currency
Class Money

java.lang.Object
  extended by com.highdeal.currency.Money
All Implemented Interfaces:
java.io.Serializable

public class Money
extends java.lang.Object
implements java.io.Serializable

Money is an immutable representing an amount of money with an arbitrary precision.

See Also:
Serialized Form

Field Summary
static int NEAREST
          Rounds toward nearest neighbor.
static int RAISE
          Increments the digit prior to a non-zero discarded fraction.
static int TRUNCATE
          Truncate the money amount.
 
Constructor Summary
Money(java.math.BigDecimal anAmount, Currency aCurrency)
          Create a new money amount for a currency and with an internal precision sets to the currency default precision.
Money(java.math.BigDecimal anAmount, Currency aCurrency, int internalPrecision)
          Create a new money amount for a currency and with an internal precision.
Money(java.math.BigDecimal anAmount, Currency aCurrency, int internalPrecision, int roundingMode)
          Create a new money amount for a currency and with an internal precision.
Money(java.math.BigDecimal anAmount, java.lang.String currencyCode)
          Create a new money amount for a currency and with an internal precision sets to the currency default precision.
Money(java.math.BigDecimal anAmount, java.lang.String currencyCode, int internalPrecision, int roundingMode)
          Create a new money amount for a currency and with an internal precision.
Money(java.lang.String s)
          Creates a new money amount by parsing the string s that must be like EUR 0.00 or USD 0.00 for instance.
 
Method Summary
 Money add(Money val)
          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.
 Money add(Money val, int precision, int roundingMode)
          Returns a money amount whose value is the sum of this and val with a specified precision and rouding mode.
 int compareTo(Money val)
          Returns 0 if amounts are equals (same currency and same value), -1, or 1.
static int convertRounding(int rounding)
          Converts a money rounding into a big decimal rounding.
 Money divide(java.math.BigDecimal val, int roundingMode)
          Returns a money amount whose value is the quotient of this by a value, rounded through a given rounding mode.
 Money divide(java.math.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.
 Money duplicate()
          Creates a duplicate of this money amount.
 boolean equals(java.lang.Object moneyAmount)
          Returns true if amounts are equals (same currency and same value), false otherwise.
 java.math.BigDecimal getAmount()
          Returns the amount for this money amount.
 Currency getCurrency()
          Returns the currency for this money amount.
static java.lang.String getRounding(int rounding)
          Returns the string representation of the rounding mode corresponding to a money rounding mode.
static int getRounding(java.lang.String rounding)
          Returns the money rounding from a string representation of the rounding.
 int hashCode()
           
static boolean isMoneyFormat(java.lang.String s)
          Tests if the String s is in money format.
 boolean isZero()
          Returns true if amount is zero, whatever the currency, false otherwise.
 Money max(Money val)
          Returns the maximum of this and val.
 Money min(Money val)
          Returns the minimum of this and val.
 Money multiply(java.math.BigDecimal val, int roundingMode)
          Returns a money amount whose value is the multiplication of this by a value, rounded through a given rounding mode.
 Money multiply(java.math.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.
 Money negate()
          Returns a money amount whose value is the opposite of this amount.
 Money round(int roundingMode)
          Returns a new money amount whose value is rounded to the precision of its currency using the specified rounding mode.
 Money round(int precision, int roundingMode)
          Returns a new money amount whose value is rounded to the specified precision and rounding mode.
 Money subtract(Money val)
          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.
 Money subtract(Money val, int precision, int roundingMode)
          Returns a money amount whose value is the difference of this and val with a specified precision and rouding mode.
 Money toEMUCurrency(Currency toCurrency)
          Converts the money amount to a EMU currency.
 Money toEuro()
          Converts the money amount to Euro.
 java.lang.String toString()
          Gives a string representation of the amount.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NEAREST

public static final int NEAREST
Rounds toward nearest neighbor. If the discarded fraction is >= .5, then raise, otherwise truncate.

See Also:
Constant Field Values

TRUNCATE

public static final int TRUNCATE
Truncate the money amount. The fraction is discarded.

See Also:
Constant Field Values

RAISE

public static final int RAISE
Increments the digit prior to a non-zero discarded fraction.

See Also:
Constant Field Values
Constructor Detail

Money

public Money(java.lang.String s)
      throws java.lang.IllegalArgumentException
Creates a new money amount by parsing the string s that must be like EUR 0.00 or USD 0.00 for instance.

Parameters:
s - the string to be parsed.
Throws:
java.lang.IllegalArgumentException - a parsing error occurs

Money

public Money(java.math.BigDecimal anAmount,
             Currency aCurrency,
             int internalPrecision)
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

public Money(java.math.BigDecimal anAmount,
             Currency aCurrency,
             int internalPrecision,
             int roundingMode)
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

public Money(java.math.BigDecimal anAmount,
             java.lang.String currencyCode)
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:
Currency.getPrecision()

Money

public Money(java.math.BigDecimal anAmount,
             java.lang.String currencyCode,
             int internalPrecision,
             int roundingMode)
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

public Money(java.math.BigDecimal anAmount,
             Currency aCurrency)
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:
Currency.getPrecision()
Method Detail

isZero

public boolean isZero()
Returns true if amount is zero, whatever the currency, false otherwise.

Returns:
true if amount is zero, false otherwise.

duplicate

public Money duplicate()
Creates a duplicate of this money amount.

Returns:
a duplicate of this money amount.

getCurrency

public Currency getCurrency()
Returns the currency for this money amount.

Returns:
the currency for this money amount.

getAmount

public java.math.BigDecimal getAmount()
Returns the amount for this money amount.

Returns:
the amount for this money amount.

negate

public Money negate()
Returns a money amount whose value is the opposite of this amount.

Returns:
the opposite amount for this money amount.

add

public Money add(Money val)
          throws IncompatibleCurrenciesException
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

public Money add(Money val,
                 int precision,
                 int roundingMode)
          throws IncompatibleCurrenciesException
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

public Money subtract(Money val)
               throws IncompatibleCurrenciesException
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

public Money multiply(java.math.BigDecimal val,
                      int roundingMode)
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

public Money multiply(java.math.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.

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

public Money divide(java.math.BigDecimal val,
                    int roundingMode)
             throws java.lang.ArithmeticException
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:
java.lang.ArithmeticException - if val is zero.

divide

public Money divide(java.math.BigDecimal val,
                    int internalPrecision,
                    int roundingMode)
             throws java.lang.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:
java.lang.ArithmeticException - if val is zero.

max

public Money max(Money val)
          throws IncompatibleCurrenciesException
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

public Money min(Money val)
          throws IncompatibleCurrenciesException
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

public boolean equals(java.lang.Object moneyAmount)
Returns true if amounts are equals (same currency and same value), false otherwise.

Overrides:
equals in class java.lang.Object
Parameters:
moneyAmount - The amount to compare with.
Returns:
true if amounts are equals, false otherwise.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

compareTo

public int compareTo(Money val)
              throws IncompatibleCurrenciesException
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

public Money round(int roundingMode)
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

public Money round(int precision,
                   int roundingMode)
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

public java.lang.String toString()
Gives a string representation of the amount.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the amount.

toEuro

public Money toEuro()
             throws IncompatibleCurrenciesException
Converts the money amount to Euro.

Returns:
the amount in euro.
Throws:
IncompatibleCurrenciesException - if currency of the money is not a EMU currency.

toEMUCurrency

public Money toEMUCurrency(Currency toCurrency)
                    throws IncompatibleCurrenciesException
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

public static boolean isMoneyFormat(java.lang.String s)
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

public static int getRounding(java.lang.String rounding)
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

public static java.lang.String getRounding(int rounding)
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.

Document Published: October 2015 (SAP CC 4.0 SP10 and Later)