Loxahatchee  v0.0.1-1346
Lox Convenience C++ Library
Lox::Money Class Referencefinal

Class to help manage currency. More...

#include <LoxMoney.hpp>

Collaboration diagram for Lox::Money:

Public Member Functions

 ~Money (void)
 Destructor. More...
 
void clear (void)
 Reset the value to $0.00. More...
 
 operator bool (void) const
 
 operator double (void) const
 
 operator long double (void) const
 
bool operator!= (const Money &rhs) const
 
Money operator* (const Lox::Money &mult) const =delete
 Multiplying money does not make sense, especially since we round to 2 decimal places. More...
 
Money operator* (const long double mult) const
 
Money operator* (const double mult) const
 
Money operator* (const size_t mult) const
 
Money operator* (const int mult) const
 
Moneyoperator*= (const Lox::Money &mult) const =delete
 Multiplying money does not make sense, especially since we round to 2 decimal places. More...
 
Moneyoperator*= (const long double mult)
 
Moneyoperator*= (const double mult)
 
Moneyoperator*= (const size_t mult)
 
Moneyoperator*= (const int mult)
 
Money operator+ (const Lox::Money &rhs) const
 
Money operator+ (const long double rhs) const
 
Money operator+ (const double rhs) const
 
Money operator+ (const size_t rhs) const
 
Money operator+ (const int rhs) const
 
Moneyoperator+= (const Lox::Money &rhs)
 
Moneyoperator+= (const long double rhs)
 
Moneyoperator+= (const double rhs)
 
Moneyoperator+= (const size_t rhs)
 
Moneyoperator+= (const int rhs)
 
Money operator- (void) const
 
Money operator- (const Lox::Money &rhs) const
 
Money operator- (const long double rhs) const
 
Money operator- (const double rhs) const
 
Money operator- (const size_t rhs) const
 
Money operator- (const int rhs) const
 
Moneyoperator-= (const Lox::Money &rhs)
 
Moneyoperator-= (const long double rhs)
 
Moneyoperator-= (const double rhs)
 
Moneyoperator-= (const size_t rhs)
 
Moneyoperator-= (const int rhs)
 
Money operator/ (const Lox::Money &div) const =delete
 Dividing money does not make sense, especially since we round to 2 decimal places. More...
 
Money operator/ (const long double div) const
 
Money operator/ (const double div) const
 
Money operator/ (const size_t div) const
 
Money operator/ (const int div) const
 
Moneyoperator/= (const Lox::Money &div)=delete
 Dividing money does not make sense, especially since we round to 2 decimal places. More...
 
Moneyoperator/= (const long double div)
 
Moneyoperator/= (const double div)
 
Moneyoperator/= (const size_t div)
 
Moneyoperator/= (const int div)
 
bool operator< (const Money &rhs) const
 
bool operator<= (const Money &rhs) const
 
bool operator== (const Money &rhs) const
 
bool operator> (const Money &rhs) const
 
bool operator>= (const Money &rhs) const
 
long double to_long_double (void) const
 Convert the currency to a built-in type. More...
 
 Money (void)
 Constructor. More...
 
 Money (const Lox::Money &rhs)
 Constructor. More...
 
 Money (std::string str)
 
 Money (const char *const &str)
 
 Money (const long double ld)
 Constructor. More...
 
 Money (const double d)
 Constructor. More...
 
 Money (const int i)
 Constructor. More...
 
std::string to_string (void) const
 Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89. More...
 
const char * c_str (void) const
 Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89. More...
 
 operator std::string (void) const
 Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89. More...
 
int dollars (void) const
 Extract part of the value. This does not round, meaning if the value is $1.99, calling dollars() wont return $2. More...
 
int cents (void) const
 Given $1.23, this will return "23" not "0.23". More...
 
void dollars_and_cents (int &d, int &c) const
 Extract part of the value. This does not round, meaning if the value is $1.99, calling dollars() wont return $2. More...
 
bool empty (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_empty (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_not_empty (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_zero (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_not_zero (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_zero_or_less (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_zero_or_more (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_less_than_zero (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 
bool is_more_than_zero (void) const
 Sometimes it is nice in an "if" clause to spell out the check. More...
 

Static Public Member Functions

static bool is_valid (std::string str)
 Determine if the given string can be interpreted as money. More...
 

Protected Attributes

std::string c_scratch
 
long double money
 
long double round_to_cents (void) const
 Get rid of everything after the cents. For example, given the value 123.456, this would return 123.46. More...
 
static long double round_to_cents (long double money)
 Get rid of everything after the cents. For example, given the value 123.456, this would return 123.46. More...
 
bool nearly_the_same (const Lox::Money &rhs) const
 Check to see if the difference is less than $0.005 (half a penny). More...
 
static bool nearly_the_same (const Lox::Money &lhs, const Lox::Money &rhs)
 Check to see if the difference is less than $0.005 (half a penny). More...
 

Detailed Description

Class to help manage currency.

Even though the underlying type is a "long double", every effort is made to round values to 2 decimal places. For example, you can construct a Money object with the initial value 4.56789, but the value is immediately converted to 4.57, not stored as 4.56789.

This is intentional!

Otherwise, on invoices and statements, if you have the number 4.56789 which is displayed to the user as "$4.57" and you either multiply or divide it, you'll get inconsistent results. For example:

    $4.57 * 10 = $45.70

...but if we stored the original number 4.56789, then it looks like:

    $4.57 * 10 = $45.68

...and the calculations look like they're off by 2 cents. No-one wants to see invoices where the amounts don't add up perfectly. So this class immediately converts all values to 2 decimal places to prevent these kinds of fractional inconsistencies.

Constructor & Destructor Documentation

Lox::Money::Money ( void  )
Lox::Money::Money ( const Lox::Money rhs)

Constructor.

Lox::Money::Money ( std::string  str)

If the string is invalid – as determined by Lox::Money::is_valid() – then the value will be zero.

References is_valid(), money, and round_to_cents().

Here is the call graph for this function:

Lox::Money::Money ( const char *const &  str)

If the string is invalid – as determined by Lox::Money::is_valid() – then the value will be zero.

Lox::Money::Money ( const long double  ld)

Constructor.

Lox::Money::Money ( const double  d)

Constructor.

Lox::Money::Money ( const int  i)

Constructor.

Lox::Money::~Money ( void  )

Destructor.

Member Function Documentation

const char* Lox::Money::c_str ( void  ) const
inline

Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89.

References c_scratch, and to_string().

Here is the call graph for this function:

int Lox::Money::cents ( void  ) const

Given $1.23, this will return "23" not "0.23".

void Lox::Money::clear ( void  )

Reset the value to $0.00.

int Lox::Money::dollars ( void  ) const

Extract part of the value. This does not round, meaning if the value is $1.99, calling dollars() wont return $2.

Given $1.23, this will return "1".

void Lox::Money::dollars_and_cents ( int &  d,
int &  c 
) const

Extract part of the value. This does not round, meaning if the value is $1.99, calling dollars() wont return $2.

Given $1.23, this will return "1".

bool Lox::Money::empty ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator==().

Here is the call graph for this function:

bool Lox::Money::is_empty ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator==().

Here is the call graph for this function:

bool Lox::Money::is_less_than_zero ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator<().

Here is the call graph for this function:

bool Lox::Money::is_more_than_zero ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator>().

Here is the call graph for this function:

bool Lox::Money::is_not_empty ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator!=().

Here is the call graph for this function:

bool Lox::Money::is_not_zero ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator!=().

Here is the call graph for this function:

bool Lox::Money::is_valid ( std::string  str)
static

Determine if the given string can be interpreted as money.

Referenced by Money().

Here is the caller graph for this function:

bool Lox::Money::is_zero ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator==().

Here is the call graph for this function:

bool Lox::Money::is_zero_or_less ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator<=().

Here is the call graph for this function:

bool Lox::Money::is_zero_or_more ( void  ) const
inline

Sometimes it is nice in an "if" clause to spell out the check.

References Money(), and operator>=().

Here is the call graph for this function:

bool Lox::Money::nearly_the_same ( const Lox::Money lhs,
const Lox::Money rhs 
)
static

Check to see if the difference is less than $0.005 (half a penny).

References money.

Referenced by operator==().

Here is the caller graph for this function:

bool Lox::Money::nearly_the_same ( const Lox::Money rhs) const
inline

Check to see if the difference is less than $0.005 (half a penny).

References nearly_the_same().

Referenced by nearly_the_same().

Here is the call graph for this function:

Here is the caller graph for this function:

Lox::Money::operator bool ( void  ) const
inlineexplicit

References Money(), and operator!=().

Here is the call graph for this function:

Lox::Money::operator double ( void  ) const
inlineexplicit

References to_long_double().

Here is the call graph for this function:

Lox::Money::operator long double ( void  ) const
inlineexplicit

References to_long_double().

Here is the call graph for this function:

Lox::Money::operator std::string ( void  ) const
inline

Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89.

References to_string().

Here is the call graph for this function:

bool Lox::Money::operator!= ( const Money rhs) const
inline

References operator==().

Referenced by is_not_empty(), is_not_zero(), and operator bool().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator* ( const Lox::Money mult) const
inlinedelete

Multiplying money does not make sense, especially since we round to 2 decimal places.

Money Lox::Money::operator* ( const long double  mult) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator* ( const double  mult) const
inline

References operator*().

Referenced by operator*().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator* ( const size_t  mult) const
inline

References operator*().

Referenced by operator*().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator* ( const int  mult) const
inline

References operator*().

Referenced by operator*().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator*= ( const Lox::Money mult) const
inlinedelete

Multiplying money does not make sense, especially since we round to 2 decimal places.

Money& Lox::Money::operator*= ( const long double  mult)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator*= ( const double  mult)
inline

References operator*=().

Referenced by operator*=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator*= ( const size_t  mult)
inline

References operator*=().

Referenced by operator*=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator*= ( const int  mult)
inline

References operator*=().

Referenced by operator*=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator+ ( const Lox::Money rhs) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator+ ( const long double  rhs) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator+ ( const double  rhs) const
inline

References operator+().

Referenced by operator+().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator+ ( const size_t  rhs) const
inline

References operator+().

Referenced by operator+().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator+ ( const int  rhs) const
inline

References operator+().

Referenced by operator+().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator+= ( const Lox::Money rhs)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator+= ( const long double  rhs)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator+= ( const double  rhs)
inline

References operator+=().

Referenced by operator+=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator+= ( const size_t  rhs)
inline

References operator+=().

Referenced by operator+=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator+= ( const int  rhs)
inline

References operator+=().

Referenced by operator+=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator- ( void  ) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator- ( const Lox::Money rhs) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator- ( const long double  rhs) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator- ( const double  rhs) const
inline

References operator-().

Referenced by operator-().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator- ( const size_t  rhs) const
inline

References operator-().

Referenced by operator-().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator- ( const int  rhs) const
inline

References operator-().

Referenced by operator-().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator-= ( const Lox::Money rhs)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator-= ( const long double  rhs)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator-= ( const double  rhs)
inline

References operator-=().

Referenced by operator-=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator-= ( const size_t  rhs)
inline

References operator-=().

Referenced by operator-=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator-= ( const int  rhs)
inline

References operator-=().

Referenced by operator-=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator/ ( const Lox::Money div) const
inlinedelete

Dividing money does not make sense, especially since we round to 2 decimal places.

Money Lox::Money::operator/ ( const long double  div) const
inline

References Money(), and money.

Here is the call graph for this function:

Money Lox::Money::operator/ ( const double  div) const
inline

References operator/().

Referenced by operator/().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator/ ( const size_t  div) const
inline

References operator/().

Referenced by operator/().

Here is the call graph for this function:

Here is the caller graph for this function:

Money Lox::Money::operator/ ( const int  div) const
inline

References operator/().

Referenced by operator/().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator/= ( const Lox::Money div)
inlinedelete

Dividing money does not make sense, especially since we round to 2 decimal places.

Money& Lox::Money::operator/= ( const long double  div)
inline

References money, and round_to_cents().

Here is the call graph for this function:

Money& Lox::Money::operator/= ( const double  div)
inline

References operator/=().

Referenced by operator/=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator/= ( const size_t  div)
inline

References operator/=().

Referenced by operator/=().

Here is the call graph for this function:

Here is the caller graph for this function:

Money& Lox::Money::operator/= ( const int  div)
inline

References operator/=().

Referenced by operator/=().

Here is the call graph for this function:

Here is the caller graph for this function:

bool Lox::Money::operator< ( const Money rhs) const
inline

References money.

Referenced by is_less_than_zero(), and operator<=().

Here is the caller graph for this function:

bool Lox::Money::operator<= ( const Money rhs) const
inline

References operator<(), and operator==().

Referenced by is_zero_or_less().

Here is the call graph for this function:

Here is the caller graph for this function:

bool Lox::Money::operator== ( const Money rhs) const
inline

References nearly_the_same().

Referenced by empty(), is_empty(), is_zero(), operator!=(), operator<=(), and operator>=().

Here is the call graph for this function:

Here is the caller graph for this function:

bool Lox::Money::operator> ( const Money rhs) const
inline

References money.

Referenced by is_more_than_zero(), and operator>=().

Here is the caller graph for this function:

bool Lox::Money::operator>= ( const Money rhs) const
inline

References operator==(), and operator>().

Referenced by is_zero_or_more().

Here is the call graph for this function:

Here is the caller graph for this function:

static long double Lox::Money::round_to_cents ( long double  money)
inlinestatic

Get rid of everything after the cents. For example, given the value 123.456, this would return 123.46.

References Lox::Numbers::round_to_2_decimals().

Here is the call graph for this function:

long double Lox::Money::round_to_cents ( void  ) const
inline

Get rid of everything after the cents. For example, given the value 123.456, this would return 123.46.

References money, and round_to_cents().

Referenced by Money(), operator*=(), operator+=(), operator-=(), operator/=(), round_to_cents(), and to_long_double().

Here is the call graph for this function:

Here is the caller graph for this function:

long double Lox::Money::to_long_double ( void  ) const
inline

Convert the currency to a built-in type.

References round_to_cents().

Referenced by operator double(), and operator long double().

Here is the call graph for this function:

Here is the caller graph for this function:

std::string Lox::Money::to_string ( void  ) const

Format the amount as a text string. E.g., the value 1234567.89 will be formatted as $1,234,567.89.

Referenced by c_str(), operator std::string(), and Lox::operator<<().

Here is the caller graph for this function:

Member Data Documentation

std::string Lox::Money::c_scratch
mutableprotected

Referenced by c_str().


The documentation for this class was generated from the following files: