All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.roguewave.money.currency.v1_0.Money

java.lang.Object
   |
   +----com.roguewave.money.currency.v1_0.Money

public class Money
extends Object
Class Money provides a mapping between an amount and a currency, by encapsulating a value (either decimal or floating point) and referencing a currency. The value is either a DoubleValue which wraps a primitive double or a BigDecimalValue which wraps java.math.BigDecimal. When Money objects are used in arithmetic operations, all operands must be of the same currency. If the currencies for Money operands used in the same equation differ, the method will cause the error handler to be invoked.

Operations between monies of different currencies may be performed using class MoneyCalculator.

Here is an example of arithmetic calculations:

 .
 .
 .
 Money x = new Money(new BigDecimal("1.23"), "USD");
 Money y = new Money(9.87d, "USD");
 Money z = new Money("7.99", "USD", 30);
 Money s = x.plus(y);  // Okay, same currencies
 s = y.minus(z);       // Error, different currencies.  Error handler called
 

Author:
Trevor Misfeldt
See Also:
Value, BigDecimalValue, DoubleValue, Currency, MoneyCalculator, DefaultErrorHandler, ErrorHandler, FactoryExample, GroupExample, Format1, Format2, MoneyCalculatorExample

Variable Index

 o DEFAULT_ACCURACY
The default accuracy, or digits to the right of the decimal point, to use for the rounding methods is '2'
 o DEFAULT_DIGIT
The default digit to use for comparison is '5'
 o DEFAULT_ROUND
The default rounding method is the plain method.
 o NO_ROUND
Don’t round.
 o ROUND_BANKERS
Bankers.
 o ROUND_DOWN
Round down.
 o ROUND_PLAIN
Plain.
 o ROUND_UP
Round up.

Constructor Index

 o Money()
Creates an empty money.
 o Money(BigDecimal, String)
Creates a new money with a decimal amount, a currency and the default precision.
 o Money(BigDecimal, String, int)
Creates a new money with a decimal amount, a currency and a precision.
 o Money(double, String)
Creates a new money with a floating point amount and a currency.
 o Money(Money)
Creates a new money with the specified money's currency and amount.
 o Money(String, String)
Creates a new money with a decimal amount, a currency and the default precision.
 o Money(String, String, int)
Creates a new money with a decimal amount, a currency and a precision.
 o Money(Value, String)
Creates a new money with a decimal or floating point value and a currency.

Method Index

 o abs()
Returns a new money which has the absolute value of this money's amount.
 o add(Money, Money)
Returns a new money which is the sum of the two money objects.
 o compare(Money)
Compares this money to the specified money.
 o divide(Money, BigDecimal)
Returns a new money which is the quotient of the money and the decimal divisor.
 o divide(Money, double)
Returns a new money which is the quotient of the money and the floating point divisor.
 o equals(Money)
Returns true if this money and the specified money are equal which requires that both the currencies and the amounts be equal.
 o getAmount()
Returns the amount.
 o getCurrency()
Returns the currency.
 o greaterThan(Money)
Returns true if this money's amount is greater than that of the specified money.
 o greaterThanOrEqual(Money)
Returns true if this money's amount is greater than or equal to the specified money.
 o greaterThanZero()
Returns true if this money's amount is greater than zero.
 o isZero()
Returns true if this money's amount is zero.
 o lessThan(Money)
Returns true if this money's amount is less than that of the specified money.
 o lessThanOrEqual(Money)
Returns true if this money's amount is less than or equal to that of the specified money.
 o lessThanZero()
Returns true if this money's amount is less than zero.
 o minus(Money)
Returns a new money which is the difference between this money and the given money.
 o multiply(Money, BigDecimal)
Returns a new money which is the product of the money and the decimal value.
 o multiply(Money, double)
Returns a new money which is the product of the money and the floating point value.
 o negate()
Returns a new money which has the negation of this money's amount.
 o notEquals(Money)
Returns true if this money and the specified money are not equal which means that either the currencies or amounts differ.
 o over(BigDecimal)
Returns a new money which is the quotient of this money divided by the decimal divisor.
 o over(double)
Returns a new money which is the quotient of this money divided by the floating point divisor.
 o plus(Money)
Returns a new money which is the sum of this money and the specified money.
 o round()
Returns a new money with the rounded value of this money.
 o round(int)
Returns a new money with the rounded value of this money using the specified accuracy.
 o round(int, short)
Returns a new money with the rounded value of this money using the specified accuracy and the desired rounding method.
 o round(int, short, int)
Returns a new money with the rounded value of this money using the specified accuracy, the desired rounding method and the specified rounding digit.
 o setAmount(BigDecimal)
Sets the amount.
 o setAmount(double)
Sets the amount.
 o setAmount(String)
Sets the amount.
 o setCurrency(String)
Sets the currency.
 o subtract(Money, Money)
Returns a new money which is the difference between the two money objects.
 o times(BigDecimal)
Returns a new money which is the product of this money and the decimal number.
 o times(double)
Returns a new money which is the product of this money and the floating point number.
 o toString()
Returns a string representation of this money.

Variables

 o NO_ROUND
 public static final short NO_ROUND
Don’t round. No rounding will occur, and accuracy is ignored.

 o ROUND_UP
 public static final short ROUND_UP
Round up. Always add one to the digit at the specified accuracy decimal place, then truncate any digits to the right.

 o ROUND_DOWN
 public static final short ROUND_DOWN
Round down. Truncate all digits to the right of the specified accuracy decimal place.

 o ROUND_PLAIN
 public static final short ROUND_PLAIN
Plain. If the digit one to the right of the specified accuracy decimal place is equal to or greater than a comparison digit, increase the digit at the accuracy position by one and truncate all numbers to the right. Otherwise, truncate all digits to the right of the accuracy place digit.

 o ROUND_BANKERS
 public static final short ROUND_BANKERS
Bankers. If the digit one to the right of the specified accuracy decimal place is equal to a comparison digit, round so that the digit at the specified accuracy position is even. If the digit one to the right of the specified accuracy position is greater than a comparison digit, increment by one the digit at the specified accuracy position. If the digit one to the right of the specified accuracy position is less than a comparison digit, truncate all numbers to the right of the accuracy position.

 o DEFAULT_ROUND
 public static final short DEFAULT_ROUND
The default rounding method is the plain method.

See Also:
ROUND_PLAIN
 o DEFAULT_DIGIT
 public static final int DEFAULT_DIGIT
The default digit to use for comparison is '5'

 o DEFAULT_ACCURACY
 public static final int DEFAULT_ACCURACY
The default accuracy, or digits to the right of the decimal point, to use for the rounding methods is '2'

Constructors

 o Money
 public Money()
Creates an empty money. Must be set with a value and a currency before it is useful.

 o Money
 public Money(String amount,
              String currency)
Creates a new money with a decimal amount, a currency and the default precision. For example,

Money money = new Money("5.92", "DEM");

Parameters:
amount - a decimal amount
currency - a currency mnemonic
See Also:
DEFAULT_PRECISION
 o Money
 public Money(String amount,
              String currency,
              int precision)
Creates a new money with a decimal amount, a currency and a precision. For example,

Money money = new Money("3.39", "FRF", 20);

Parameters:
amount - a decimal amount
currency - a currency mnemonic
precision - a precision (total number of significant digits)
 o Money
 public Money(BigDecimal amount,
              String currency)
Creates a new money with a decimal amount, a currency and the default precision. For example,

Money money = new Money(new BigDecimal("2.44"), "CAD");

Parameters:
amount - a decimal amount
currency - a currency mnemonic
See Also:
DEFAULT_PRECISION
 o Money
 public Money(BigDecimal amount,
              String currency,
              int precision)
Creates a new money with a decimal amount, a currency and a precision. For example,

Money money = new Money(new BigDecimal("2342", "JPY", 40);

Parameters:
amount - a decimal amount
currency - a currency mnemonic
precision - a precision (total number of significant digits)
 o Money
 public Money(double amount,
              String currency)
Creates a new money with a floating point amount and a currency. For example,

Money money = new Money(329.93, "GBP");

Parameters:
amount - a floating point amount
currency - a currency mnemonic
 o Money
 public Money(Value amount,
              String currency)
Creates a new money with a decimal or floating point value and a currency. For example,

Money money = new Money(new BigDecimalValue("3223"), "ITL");

Parameters:
amount - a decimal amount
currency - a currency mnemonic
See Also:
Value, BigDecimalValue, DoubleValue
 o Money
 public Money(Money money)
Creates a new money with the specified money's currency and amount.

Methods

 o getAmount
 public Value getAmount()
Returns the amount.

Returns:
this money's amount
 o setAmount
 public void setAmount(String amount)
Sets the amount. Deemed to be in the units of the current currency.

Parameters:
amount - a decimal amount
 o setAmount
 public void setAmount(BigDecimal amount)
Sets the amount. Deemed to be in the units of the current currency.

Parameters:
amount - a decimal amount
 o setAmount
 public void setAmount(double amount)
Sets the amount. Deemed to be in the units of the current currency.

Parameters:
amount - a floating point amount
 o getCurrency
 public String getCurrency()
Returns the currency.

Returns:
the currency mnemonic
 o setCurrency
 public void setCurrency(String currency)
Sets the currency.

Parameters:
currency - the currency mnemonic
 o plus
 public Money plus(Money money)
Returns a new money which is the sum of this money and the specified money. Calls the error handler if the currencies do not match.

Parameters:
money - the operand
Returns:
the sum
 o add
 public static Money add(Money money1,
                         Money money2)
Returns a new money which is the sum of the two money objects. Calls the error handler if the currencies do not match.

Parameters:
money1 - operand #1
money2 - operand #2
Returns:
the sum
 o minus
 public Money minus(Money money)
Returns a new money which is the difference between this money and the given money. Calls the error handler if the currencies do not match.

Parameters:
money - the operand
Returns:
the difference
 o subtract
 public static Money subtract(Money money1,
                              Money money2)
Returns a new money which is the difference between the two money objects. Calls the error handler if the currencies do not match.

Parameters:
money1 - operand #1
money2 - operand #2
Returns:
the difference
 o times
 public Money times(double amount)
Returns a new money which is the product of this money and the floating point number.

Parameters:
amount - the floating point operand
Returns:
the product
 o multiply
 public static Money multiply(Money money,
                              double amount)
Returns a new money which is the product of the money and the floating point value.

Parameters:
money - the money operand
amount - the floating point operand
Returns:
the product
 o times
 public Money times(BigDecimal amount)
Returns a new money which is the product of this money and the decimal number.

Parameters:
amount - the decimal operand
Returns:
the product
 o multiply
 public static Money multiply(Money money,
                              BigDecimal amount)
Returns a new money which is the product of the money and the decimal value.

Parameters:
money - the money operand
amount - the decimal operand
Returns:
the product
 o over
 public Money over(double amount)
Returns a new money which is the quotient of this money divided by the floating point divisor.

Parameters:
amount - the floating point divisor
Returns:
the quotient
 o divide
 public static Money divide(Money money,
                            double amount)
Returns a new money which is the quotient of the money and the floating point divisor.

Parameters:
money - the money operand
amount - the floating point divisor
Returns:
the quotient
 o over
 public Money over(BigDecimal amount)
Returns a new money which is the quotient of this money divided by the decimal divisor.

Parameters:
amount - the decimal divisor
Returns:
the quotient
 o divide
 public static Money divide(Money money,
                            BigDecimal amount)
Returns a new money which is the quotient of the money and the decimal divisor.

Parameters:
money - the money operand
amount - the decimal divisor
Returns:
the quotient
 o equals
 public boolean equals(Money money)
Returns true if this money and the specified money are equal which requires that both the currencies and the amounts be equal.

Parameters:
money - the money for comparison
Returns:
whether this money is equal to the specified money or not
 o notEquals
 public boolean notEquals(Money money)
Returns true if this money and the specified money are not equal which means that either the currencies or amounts differ.

Parameters:
money - the money for comparison
Returns:
whether this money is not equal to the specified money
 o lessThan
 public boolean lessThan(Money money)
Returns true if this money's amount is less than that of the specified money. Otherwise, returns false. If the currencies are not equal then the error handler is called.

Parameters:
money - the money for comparison
Returns:
whether this money is less than the specified money
 o greaterThan
 public boolean greaterThan(Money money)
Returns true if this money's amount is greater than that of the specified money. Otherwise, returns false. If the currencies are not equal then the error handler is called.

Parameters:
money - the money for comparison
Returns:
whether this money is greater than the specified money
 o lessThanOrEqual
 public boolean lessThanOrEqual(Money money)
Returns true if this money's amount is less than or equal to that of the specified money. Otherwise, returns false. If the currencies are not equal then the error handler is called.

Parameters:
money - the money for comparison
Returns:
whether this money is less than or equal to the specified money
 o greaterThanOrEqual
 public boolean greaterThanOrEqual(Money money)
Returns true if this money's amount is greater than or equal to the specified money. Otherwise, returns false. If the currencies are not equal then the error handler is called.

Parameters:
money - the money for comparison
Returns:
whether this money is greater than or equal to the specified money
 o compare
 public int compare(Money money)
Compares this money to the specified money. If the two currencies are not equal the error handler is called.

Parameters:
money - the money for comparison
Returns:
-1 if this money is less than the other money, 0 if they are equal, and 1 if it is greater
 o isZero
 public boolean isZero()
Returns true if this money's amount is zero.

Returns:
whether this money's amount equals zero or not
 o lessThanZero
 public boolean lessThanZero()
Returns true if this money's amount is less than zero.

Returns:
whether this money's amount is less than zero or not
 o greaterThanZero
 public boolean greaterThanZero()
Returns true if this money's amount is greater than zero.

Returns:
whether this money's amount is greater than zero or not
 o abs
 public Money abs()
Returns a new money which has the absolute value of this money's amount.

Returns:
the absolute value
 o negate
 public Money negate()
Returns a new money which has the negation of this money's amount.

Returns:
the negation
 o round
 public Money round()
Returns a new money with the rounded value of this money. Rounding behavior will follow the defaults for accuracy, method and digit.

Returns:
the rounded money
See Also:
DEFAULT_ACCURACY, DEFAULT_ROUND, DEFAULT_DIGIT
 o round
 public Money round(int accuracy)
Returns a new money with the rounded value of this money using the specified accuracy. The rounding method and the rounding digit will be determined by the defaults.

Parameters:
accuracy - the desired number of digits following the decimal point
Returns:
the rounded money
See Also:
DEFAULT_ROUND, DEFAULT_DIGIT
 o round
 public Money round(int accuracy,
                    short method)
Returns a new money with the rounded value of this money using the specified accuracy and the desired rounding method. The rounding digit will be determined by default.

Parameters:
accuracy - the desired number of digits following the decimal point
method - the desired rounding method
Returns:
the rounded money
See Also:
ROUND_PLAIN, ROUND_UP, ROUND_DOWN, ROUND_BANKERS, NO_ROUND, DEFAULT_DIGIT
 o round
 public Money round(int accuracy,
                    short method,
                    int digit)
Returns a new money with the rounded value of this money using the specified accuracy, the desired rounding method and the specified rounding digit.

Parameters:
accuracy - the desired number of digits following the decimal point
method - the desired rounding method
digit - the digit to round on
Returns:
the rounded money
See Also:
ROUND_PLAIN, ROUND_UP, ROUND_DOWN, ROUND_BANKERS, NO_ROUND
 o toString
 public String toString()
Returns a string representation of this money. Format looks like: USD 4.98

Returns:
the string
Overrides:
toString in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index