public class Money extends Object implements Cloneable, Serializable
The Money class represents a United States monetary value, expressed in dollars and cents. Internally, the value is represented using Java's BigDecimal class.
Methods are provided to perform all the usual arithmetic manipulations required when dealing with monetary data, including add, subtract, multiply, and divide.
Rounding
Rounding does not occur during intermediate computations; maximum precision (and accuracy) is thus preserved throughout all computations. Round-off to an integral cent occurs only when the monetary value is externalized (when formatted for display as a String or converted to a long integer). One of several different rounding modes can be specified. The default rounding mode is to discard any fractional cent and truncate the monetary value to 2 decimal places.
Currency Format
A Currency Format (an instance of DecimalFormat) is used to control formatting of monetary values for display as well as the parsing of strings which represent monetary values. By default, the Currency Format for the current locale is used. For the United States, the default Currency Symbol is the Dollar Sign ("$"). Negative amounts are enclosed in parentheses. A Decimal Point (".") separates the dollars and cents, and a comma (",") separates each group of 3 consecutive digits in the dollar amount.
Examples: $1,234.56 ($1,234.56)
Immutability
Money objects, like String objects, are immutable. An operation on a Money object (such as add, subtract, etc.) does not alter the object in any way. Rather, a new Money object is returned whose state reflects the result of the operation. Thus, a statement like
money1.add(money2);
has no effect; it does not modify money1 in any way, and the result is effectively discarded. If the intent is to modify money1, then you should code
money1 = money1.add(money2);
which effectively replaces money1 with the result.
BigDecimal,
DecimalFormat,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
Money.InvalidRoundingModeException
Class InvalidRoundingModeException
|
static class |
Money.InvalidScaleFactorException
Class InvalidScaleFactorException
|
| Modifier and Type | Field and Description |
|---|---|
protected DecimalFormat |
currencyFormat
The Currency Format, used for formatting and parsing a
monetary value.
|
protected int |
roundingMode
The Rounding Mode.
|
protected BigDecimal |
value
The monetary value.
|
protected static BigDecimal |
ZERO
The special monetary value of zero ($0.00).
|
| Constructor and Description |
|---|
Money()
Default Constructor for a Money object; creates an object
whose value is $0.00.
|
Money(BigDecimal amount)
Constructs a Money object from a BigDecimal object.
|
Money(double amount)
Constructs a Money object from a double-precision,
floating-point value.
|
Money(long amount)
Constructs a Money object from a long integer value.
|
Money(long amount,
int scale)
Constructs a Money object from a long integer value with a
specified scale factor.
|
Money(Money amount)
Copy Constructor; constructs a Money object from another
Money object.
|
Money(String string)
Constructs a Money object from a string representation of a
monetary value.
|
| Modifier and Type | Method and Description |
|---|---|
Money |
abs()
Returns the absolute monetary value.
|
Money |
add(Money money)
Adds a specified monetary value to this monetary value.
|
Object |
clone()
Clones a Money object.
|
Money |
divide(double div)
Divides this monetary value by a specified value.
|
Money |
divide(long div)
Divides this monetary value by a specified value.
|
boolean |
equals(Object object)
Compares this object with the specified object.
|
DecimalFormat |
getCurrencyFormat()
Returns the Currency Format, used to format and parse
monetary values.
|
int |
getRoundingMode()
Returns the Rounding Mode.
|
BigDecimal |
getValue()
Returns the monetary value as a BigDecimal object.
|
int |
hashCode()
Returns a hashcode for this object.
|
boolean |
isEqual(Money other)
Returns an indication of whether or not this monetary value
is equal to
another monetary value.
|
boolean |
isGreaterThan(Money other)
Returns an indication of whether or not this monetary value
is greater than
another monetary value.
|
boolean |
isGreaterThanOrEqual(Money other)
Returns an indication of whether or not this monetary value
is greater than
or equal to another monetary value.
|
boolean |
isLessThan(Money other)
Returns an indication of whether or not this monetary value
is less than
another monetary value.
|
boolean |
isLessThanOrEqual(Money other)
Returns an indication of whether or not this monetary value
is less than
or equal to another monetary value.
|
boolean |
isNegative()
Returns an indication of whether or not this monetary value
is negative (less than $0.00).
|
boolean |
isPositive()
Returns an indication of whether or not this monetary value
is positive (greater than or equal to $0.00).
|
boolean |
isZero()
Returns an indication of whether or not this monetary value
is zero (equal to $0.00).
|
Money |
multiply(double mult)
Multiplies this monetary value by a specified value.
|
Money |
multiply(long mult)
Multiplies this monetary value by a specified value.
|
Money |
negate()
Negates this monetary value.
|
Money |
parse(String string)
Parses a string representation of a monetary value, returning
a new Money
object with the specified value.
|
Money |
setCurrencyFormat(DecimalFormat format)
Sets the Currency Format, used for formatting and parsing
monetary values.
|
Money |
setRoundingMode(int mode)
Sets the Rounding Mode.
|
Money |
subtract(Money money)
Subtracts a specified monetary value from this monetary value.
|
double |
toDouble()
Returns the monetary value as a double-precision,
floating-point value.
|
long |
toLong()
Returns the monetary value as a long integer with 2 decimal
digits to the
right of an implicit decimal point.
|
String |
toString()
Returns a formatted string representation of the monetary
value.
|
protected BigDecimal value
protected int roundingMode
protected DecimalFormat currencyFormat
protected static final BigDecimal ZERO
public Money()
public Money(double amount)
The integral part of the value represents whole dollars, and the fractional part of the value represents fractional dollars (cents). As an example, the value 19.95 would represent $19.95.
amount - The monetary amount, in dollars
and cents
public Money(long amount)
amount - The monetary amount, in whole
dollars (no cents)
public Money(long amount,
int scale)
throws Money.InvalidScaleFactorException
For example, the value 1995 would be interpreted as a monetary value of $1995.00, $199.50, and $19.95 for scale factors of 0, 1, or 2, respectively.
amount - The monetary amount, in dollars
and centsscale - Scale factor (must be 0, 1, or 2)
Money.InvalidScaleFactorException - The scale factor
specified is not valid (must be 0, 1, or 2)
public Money(String string) throws ParseException
string - A String representing a monetary
value
ParseException - The string is inconsistent with
the Currency Format
DecimalFormat,
setCurrencyFormat(java.text.DecimalFormat)public Money(BigDecimal amount)
amount - A BigDecimal value representing a
monetary amount, in dollars and
cents
public Money(Money amount)
amount - A Money object
public Money add(Money money)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
money - The monetary value to be added
public Money subtract(Money money)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
money - The monetary value to be subtracted
public Money multiply(double mult)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
mult - The multiplier value
public Money multiply(long mult)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
mult - The multiplier value
public Money divide(double div)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
div - The divisor value
public Money divide(long div)
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
div - The divisor value
public Money negate()
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
public Money abs()
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
public long toLong()
public double toDouble()
Note: Exercise care when converting monetary values to floating point values, because floating-point arithmetic is not well-suited for use with monetary data.
public BigDecimal getValue()
public int getRoundingMode()
BigDecimal.ROUND_UP,
BigDecimal.ROUND_DOWN,
BigDecimal.ROUND_CEILING,
BigDecimal.ROUND_FLOOR,
BigDecimal.ROUND_HALF_UP,
BigDecimal.ROUND_HALF_DOWN,
BigDecimal.ROUND_HALF_EVENpublic Money setRoundingMode(int mode) throws Money.InvalidRoundingModeException
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
int - The Rounding Mode
Money.InvalidRoundingModeException - The Rounding Mode
specified is not valid for monetary data
BigDecimal.ROUND_UP,
BigDecimal.ROUND_DOWN,
BigDecimal.ROUND_CEILING,
BigDecimal.ROUND_FLOOR,
BigDecimal.ROUND_HALF_UP,
BigDecimal.ROUND_HALF_DOWN,
BigDecimal.ROUND_HALF_EVENpublic DecimalFormat getCurrencyFormat()
DecimalFormatpublic Money setCurrencyFormat(DecimalFormat format)
By default, the Currency Format for the current locale is used. For the United States, the default Currency Symbol is the Dollar Sign ("$"). Negative amounts are enclosed in parentheses. A Decimal Point (".") separates the dollars and cents, and a comma (",") separates each group of 3 consecutive digits in the dollar amount.
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
format - The Currency Format
DecimalFormatpublic String toString()
By default, the Currency Format for the current locale is used.
toString in class ObjectDecimalFormat,
setCurrencyFormat(java.text.DecimalFormat)public Money parse(String string) throws ParseException
The value of this object is not affected in any way. A new object is returned reflecting the result of the operation.
string - A String representing a monetary
value
ParseException - The string is inconsistent with
the Currency Format
DecimalFormat,
setCurrencyFormat(java.text.DecimalFormat)public boolean isZero()
public boolean isNegative()
public boolean isPositive()
public boolean isEqual(Money other)
other - A monetary value with which this
monetary value is to be compared
public boolean isLessThan(Money other)
other - A monetary value with which this
monetary value is to be compared
public boolean isLessThanOrEqual(Money other)
other - A monetary value with which this
monetary value is to be compared
public boolean isGreaterThan(Money other)
other - A monetary value with which this
monetary value is to be compared
public boolean isGreaterThanOrEqual(Money other)
other - A monetary value with which this
monetary value is to be compared
public boolean equals(Object object)
public int hashCode()
Copyright © 2002–2019 Starter Inc.. All rights reserved.