Under reconstruction—2016!

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

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

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.


Author:

Conrad Weisert, Information Disciplines, Inc., Chicago

Purpose:

Usage:

  1. #include "Money.hpp" before using any of the methods described below.
  2. Link-edit with Money.cpp

Constructors:

   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) . . . 

Accessors:

   amt.MoneyInt()          //  Whole dollar (or other unit) portion (double)
   amt.MoneyCents()        //  Decimal fraction portion (short) 

Arithmetic and relational operators

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.

Stream I-O


Global constants:

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.

Output formatting constants

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

To change the internal representation


Return to Freeware directory
Return to IDI home page
Last modified 22 November, 1999