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
-
DEFAULT_ACCURACY
- The default accuracy, or digits to the right of the decimal point, to use for
the rounding methods is '
2
'
-
DEFAULT_DIGIT
- The default digit to use for comparison is '
5
'
-
DEFAULT_ROUND
- The default rounding method is the plain method.
-
NO_ROUND
- Don’t round.
-
ROUND_BANKERS
- Bankers.
-
ROUND_DOWN
- Round down.
-
ROUND_PLAIN
- Plain.
-
ROUND_UP
- Round up.
-
Money()
- Creates an empty money.
-
Money(BigDecimal, String)
- Creates a new money with a decimal amount, a currency and the default precision.
-
Money(BigDecimal, String, int)
- Creates a new money with a decimal amount, a currency and a precision.
-
Money(double, String)
- Creates a new money with a floating point amount and a currency.
-
Money(Money)
- Creates a new money with the specified money's currency and amount.
-
Money(String, String)
- Creates a new money with a decimal amount, a currency and the default precision.
-
Money(String, String, int)
- Creates a new money with a decimal amount, a currency and a precision.
-
Money(Value, String)
- Creates a new money with a decimal or floating point value and a currency.
-
abs()
- Returns a new money which has the absolute value of this money's amount.
-
add(Money, Money)
- Returns a new money which is the sum of the two money objects.
-
compare(Money)
- Compares this money to the specified money.
-
divide(Money, BigDecimal)
- Returns a new money which is the quotient of the money and the decimal
divisor.
-
divide(Money, double)
- Returns a new money which is the quotient of the money and the floating
point divisor.
-
equals(Money)
- Returns true if this money and the specified money are equal which requires that
both the currencies and the amounts be equal.
-
getAmount()
- Returns the amount.
-
getCurrency()
- Returns the currency.
-
greaterThan(Money)
- Returns true if this money's amount is greater than that of the specified money.
-
greaterThanOrEqual(Money)
- Returns true if this money's amount is greater than or equal to the
specified money.
-
greaterThanZero()
- Returns true if this money's amount is greater than zero.
-
isZero()
- Returns true if this money's amount is zero.
-
lessThan(Money)
- Returns true if this money's amount is less than that of the specified money.
-
lessThanOrEqual(Money)
- Returns true if this money's amount is less than or equal to that of the
specified money.
-
lessThanZero()
- Returns true if this money's amount is less than zero.
-
minus(Money)
- Returns a new money which is the difference between this money and the given money.
-
multiply(Money, BigDecimal)
- Returns a new money which is the product of the money and the decimal value.
-
multiply(Money, double)
- Returns a new money which is the product of the money and the floating point value.
-
negate()
- Returns a new money which has the negation of this money's amount.
-
notEquals(Money)
- Returns true if this money and the specified money are not equal which means
that either the currencies or amounts differ.
-
over(BigDecimal)
- Returns a new money which is the quotient of this money divided by the decimal
divisor.
-
over(double)
- Returns a new money which is the quotient of this money divided by the floating
point divisor.
-
plus(Money)
- Returns a new money which is the sum of this money and the specified money.
-
round()
- Returns a new money with the rounded value of this money.
-
round(int)
- Returns a new money with the rounded value of this money using the specified accuracy.
-
round(int, short)
- Returns a new money with the rounded value of this money using the specified
accuracy and the desired rounding method.
-
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.
-
setAmount(BigDecimal)
- Sets the amount.
-
setAmount(double)
- Sets the amount.
-
setAmount(String)
- Sets the amount.
-
setCurrency(String)
- Sets the currency.
-
subtract(Money, Money)
- Returns a new money which is the difference between the two money objects.
-
times(BigDecimal)
- Returns a new money which is the product of this money and the decimal
number.
-
times(double)
- Returns a new money which is the product of this money and the floating point
number.
-
toString()
- Returns a string representation of this money.
NO_ROUND
public static final short NO_ROUND
- Don’t round. No rounding will occur, and accuracy is ignored.
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.
ROUND_DOWN
public static final short ROUND_DOWN
- Round down. Truncate all digits to the right of the specified accuracy decimal place.
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.
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.
DEFAULT_ROUND
public static final short DEFAULT_ROUND
- The default rounding method is the plain method.
- See Also:
- ROUND_PLAIN
DEFAULT_DIGIT
public static final int DEFAULT_DIGIT
- The default digit to use for comparison is '
5
'
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
'
Money
public Money()
- Creates an empty money. Must be set with a value and a currency before it is useful.
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
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)
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
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)
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
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
Money
public Money(Money money)
- Creates a new money with the specified money's currency and amount.
getAmount
public Value getAmount()
- Returns the amount.
- Returns:
- this money's amount
setAmount
public void setAmount(String amount)
- Sets the amount. Deemed to be in the units of the current currency.
- Parameters:
- amount - a decimal amount
setAmount
public void setAmount(BigDecimal amount)
- Sets the amount. Deemed to be in the units of the current currency.
- Parameters:
- amount - a decimal amount
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
getCurrency
public String getCurrency()
- Returns the currency.
- Returns:
- the currency mnemonic
setCurrency
public void setCurrency(String currency)
- Sets the currency.
- Parameters:
- currency - the currency mnemonic
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
isZero
public boolean isZero()
- Returns true if this money's amount is zero.
- Returns:
- whether this money's amount equals zero or not
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
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
abs
public Money abs()
- Returns a new money which has the absolute value of this money's amount.
- Returns:
- the absolute value
negate
public Money negate()
- Returns a new money which has the negation of this money's amount.
- Returns:
- the negation
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
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
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
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
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