Money type (Java Class Definition)| This special freeware version does not depend on any other IDI library
items. It supports only a single (but customizable) internal representation
and a single type of currency at a time (default=U.S.)
Note that none of Java's built-in or primitive data types is satisfactory for money data, either because of insufficient range or because of rounding error. Therefore, this class or a similar one is required in almost all business applications written in Java. |
Some writers (e.g. Joshua Bloch)
recommend using Java's standard BigDecimal
class for monetary calculations. Compared with this or another well-designed
Money class, however,
BigDecimal presents two
serious disadvantages:
|
Conrad Weisert, Information Disciplines, Inc., Chicago
First obtain and compile that source code file Money.java
Money amt1 = new Money(); // Default constructor -- initial value is $0.00
Money amt2 = new Money(float_expr); // Conversion from double float
Money amt3 = new Money(Money_expr); // Copy constructor
long amt.wholeUnits() // Whole dollar (or other unit) portion
short amt.cents() // Decimal fraction portion
Since Java doesn't support operator overloading and since arithmetic
and relational operations are fundamental to working with money data,
we follow the naming convention proposed in Conventions for Arithmetic
Operations in Java. By analogy to the corresponding C++ class Money
supports:
Money.toString function, called explicitly
(or when a Money object appears in a string context implicitly)
yields the common external representation:
$39,448,033.50 (if U.S.) Various static constants make it easy to customize the Money class for foreign currencies or special platforms. They're intended for making a standard version within an organization or for a major project, and should rarely if ever be changed in individual programs.
These constants control the format produced by the toString function, described above.
| Name | Purpose | Default value |
Money.pfx_symbol |
Symbol to be printed to the left of the money amount. | "$" |
Money.sfx_symbol |
Symbol to be printed to the right of the money amount. | "" |
Money.decimal_point |
character to separate whole units from 1/100ths. | '.' |
Money.group_separator |
character to separate groups of three digits. | ',' |
Money.unit_name |
full name of monetary unit | "dollar" |
Money.cent_name |
name of fractional unit | "cent" |
Example: to initialize the class to format Norwegian money:
Money.pfx_symbol = "";
Money.sfx_symbol = "Nkr"
Money.decimal_point = ',';
Money.group_separator= '.';
Money.unit_name = "krone";
Money.cent_name = "øre";
The default scaling assumes that the smallest value is one cent (or 1/100 of the unit). When the constructor converts a floating point value to a Money object it multiplies by 100, thus avoiding fractional round-off errors ind computations.
If you need to keep track of finer amounts of money you can set the
global constant Money.scale to a different value.
For example, to handle both pennies and 8ths of a dollar (used in some
stock prices) Money.scale=200;, or to handle mills
Money.scale=1000. Naturally, you must do this
before creating any Money objects.
Because Java lacks any facility for localizing a type, you must modify
the code. The default internal representation is a long
integer.
Comparable interface The compareTo method is
supported, but the implements clause is commented out,
because many Java implementations don't recognize it yet. Remove the
comment delimiters if you need to need to use standard methods (e.g.
sort) that need it.
Object methods The equals and hashCode methods
are supported for the sake of complying with some silly conventions advocated
by Java gurus. It's unlikely you'd ever want to use them. Note that
Money also supports a class-specific equals
method.
Return to Freeware directory
Return to IDI home page
Last modified 20 July, 2002