Money type (C++ 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 C'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 C++.
A series of articles in the C/C++ Users' Journal addressed conversion
of money amounts to external representations, without proposing a
practical Money class. That publication rejected several articles we
submitted that presented a Money class or discussed
aspects of it.
This class was cited, with our permission, in Problem Solving, Abstraction, and Design Using C++ by Frank Friedman and Elliot Koffmnan, 1997, Addison-Wesley, ISBN 0-201-88337-6.
Conrad Weisert, Information Disciplines, Inc., Chicago
Money amt1; // Default constructor -- initial value is $0.00 Money amt2=float_expr; // Conversion from double float Money amt3=Money_expr; // Copy constructor
Note that the conversion constructor makes it possible to use literal
constants in the traditional format, e.g.
if (price > 49.95) . . .
amt.MoneyInt() // Whole dollar (or other unit) portion (double) amt.MoneyCents() // Decimal fraction portion (short)
Money objects, or
Money object and a pure number
Money object or a
pure (double float) number.
The modulus operators amt1%amt2 and
amt1%n are not supported in this freeware version.
Note that the existence of the conversion constructor legitimizes addition or subtraction of a pure number and a money object.
s << Money_expr
yields the common external representation:
$39,448,033.50 (if U.S.)
>>) is not supported.
Read a floating point number and use the conversion constructor above.
Various static and macro 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 output-stream insertion operator, described above.
| Name | Purpose | Default value set in Money.cpp |
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 French money:
Money::pfx_symbol = "";
Money::sfx_symbol = "f";
Money::decimal_point = ',';
Money::group_separator= '.';
Money::unit_name = "franc";
Money::cent_name = "centime";
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 in 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.
The default internal representation is a double
number. This was chosen because double is the only primitive type that
supports sufficient range in certain popular implementations (Microsoft, Borland).
If the platform supports efficient 64-bit long integers, for
example, you can override the default by placing a #define
before the #include, like this:
#define MoneyIType long
#include "money.hpp"
That is, in fact, the default representation in IDI's equivalent
Java class , since Java guarantees 64-bit long integers.
For compatibility with older files, some users prefer to use a packed decimal
class, which is interpretive and very slow on many modern computers.
Return to Freeware directory
Return to IDI home page
Last modified 22 November, 1999