Moneytype (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
#include "Money.hpp"before using any of the methods described below.
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)
Moneyobject and a pure number
Moneyobject or a pure (double float) number.
The modulus operators
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_expryields the common external representation:
>>) 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.
||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 French money:
Money::pfx_symbol = "";
Money::sfx_symbol = "f";
Money::decimal_point = ',';
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
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
Naturally, you must do this before creating any
The default internal representation is a
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
#include, like this:
#define MoneyIType long
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