IDI Software Component Library
© 1996 Information Disciplines, Inc., Chicago

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:
  • Slow, interpretive arithmetic.
  • Lack of type safety and units integrity, two major reasons for choosing the object paradigm.

Author:

Conrad Weisert, Information Disciplines, Inc., Chicago

Purpose:

Usage: (public methods)

First obtain and compile that source code file Money.java

Constructors:

   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

Accessors:

   long  amt.wholeUnits()    //  Whole dollar (or other unit) portion 
short amt.cents() // Decimal fraction portion

Arithmetic and relational operators

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:

Stream I-O and conversion to string


Global constants:

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.

Output formatting constants

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";

To change the internal representation


Standard library compatibility notes

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