Moneytype (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
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.toStringfunction, called explicitly (or when a Money object appears in a string context implicitly) yields the common external representation:
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.
||Symbol to be printed to the left of the money amount.||
||Symbol to be printed to the right of the money amount.||
||character to separate whole units from 1/100ths.||
||character to separate groups of three digits.||
||full name of monetary unit||
||name of fractional unit||
Example: to initialize the class to format Norwegian money:
Money.pfx_symbol = "";
Money.sfx_symbol = "Nkr"
Money.decimal_point = ',';
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
Money.scale to a different value.
For example, to handle both pennies and 8ths of a dollar (used in some
Money.scale=200;, or to handle mills
Money.scale=1000. Naturally, you must do this
before creating any
Because Java lacks any facility for localizing a type, you must modify
the code. The default internal representation is a
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.
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
Return to Freeware directory
Return to IDI home page
Last modified 20 July, 2002