// Money Class // ----------- // Objects of this class are amounts of money in a standard currency // (default = U.S.) #ifndef MONEY #define MONEY const Money #include "global.hpp" #include class Money { // Internal representation: An integer, scaled so that unity is // ----------------------- the smallest measurable quantity double value; // Floating-point to support required range. // A 64-bit integer, if available, is a more // efficient alternative. public: static double scale; // Smallest fraction of monetary unit // represented e.g. 100 = cents, 1000 = mils // (Default is 100 -- user can override.) // External representation: Constants used in output and input functions // ----------------------- (initialized in Money.cpp -- user can override) public: static char pfx_symbol[]; // Leading currency symbol (U.S.: "$") static char sfx_symbol[]; // Trailing currency symbol (U.S.: "") static char decimal_point; // Character for 100ths (U.S.: period) static char group_separator;// Character for 1000nds (U.S.: comma) static char unit_name[]; // Name of monetary unit (U.S.: "dollar") static char cent_name[]; // Name of fraction unit (U.S.: "cent") // Constructors: To support literal constants, we allow conversion from // ------------ float. This inhibits detection of some mixed expressions. private: static double round(DOUBLE x); // Assures exact conversion from float public: Money(DOUBLE x) : value(round(x * scale)) {} Money() {} // Default constructor for efficiency // The compiler will supply appropriate versions of: // - the destructor, // - the copy constructor, // - the assignment operator. // Accessor functions to separate whole and fractional parts: // --------------------------------------------------------- public: short cents() const {double dummy; return short(modf((value + (value < 0 ? -.5 :.5)) / scale, &dummy) * 100);} double wholeUnits() const {double dummy; return modf(double(value) / scale, &dummy),dummy;} // Additive pattern arithmetic operators // ------------------------------------- #define Class Money #include "Additive.hpp" #undef Class // Relational member operators: (others in global.hpp) // --------------------------- bool operator== (MONEY rs) const {return value == rs.value;} bool operator< (MONEY rs) const {return value < rs.value;} bool operator== (DOUBLE rs) const {return value == rs * scale;} bool operator< (DOUBLE rs) const {return value < rs * scale;} friend bool operator< (DOUBLE ls, MONEY rs){return ls*scale < rs.value;} }; // ********** End of class definition inline bool operator== (DOUBLE ls, MONEY rs){return rs == ls;} ostream& operator<< (ostream& ls, MONEY rs); #endif