The C++ Money class described below still works, but it's not compatible with Java and C# versions. When we developed it nearly 20 years ago, many C++ compilers didn't support 64-bit integers, so we adopted double float as our internal representation. That works, but provides somewhat less range and runs slower than a reasonable 64-bit representation.
So, we're revising it to use the same 64-bit internal representation that our Java and C# versions support. Then files created by any of the three languages will be readable by programs written in any of the others.
Preliminary C# version
We recently prepared an experimental C# version of our Money class. It lacks the formatting and international flexibility of the C++ version, but it serves to demonstrate, despite widespread misinformation, that a 64-bit representation is suitable.
That turns out to be a surprisingly controversial conclusion, as other C# libraries are now encouraging an extravagant 128-bit Money representation! Indeed C#'s standard decimal type seems to have been inspired by that wasteful practice.
After we get the revised C++ version installed we'll add more features to the C# version for full compatibility. Meanwhile we welcome suggestions for improvement and reports of any errors or other problems.
—Conrad Weisert, December, 2015
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