Class to help manage currency. More...
#include <LoxMoney.hpp>
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 |
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) |
Money & | operator*= (const double mult) |
Money & | operator*= (const size_t mult) |
Money & | operator*= (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 |
Money & | operator+= (const Lox::Money &rhs) |
Money & | operator+= (const long double rhs) |
Money & | operator+= (const double rhs) |
Money & | operator+= (const size_t rhs) |
Money & | operator+= (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 |
Money & | operator-= (const Lox::Money &rhs) |
Money & | operator-= (const long double rhs) |
Money & | operator-= (const double rhs) |
Money & | operator-= (const size_t rhs) |
Money & | operator-= (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 |
Money & | operator/= (const Lox::Money &div)=delete |
Dividing money does not make sense, especially since we round to 2 decimal places. More... | |
Money & | operator/= (const long double div) |
Money & | operator/= (const double div) |
Money & | operator/= (const size_t div) |
Money & | operator/= (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... | |
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.
Lox::Money::Money | ( | void | ) |
Constructor.
Referenced by empty(), is_empty(), is_less_than_zero(), is_more_than_zero(), is_not_empty(), is_not_zero(), is_zero(), is_zero_or_less(), is_zero_or_more(), operator bool(), operator*(), operator+(), operator-(), and operator/().
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().
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.
|
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().
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".
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator==().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator==().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator<().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator>().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator!=().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator!=().
|
static |
Determine if the given string can be interpreted as money.
Referenced by Money().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator==().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator<=().
|
inline |
Sometimes it is nice in an "if" clause to spell out the check.
References Money(), and operator>=().
|
static |
Check to see if the difference is less than $0.005 (half a penny).
References money.
Referenced by operator==().
|
inline |
Check to see if the difference is less than $0.005 (half a penny).
References nearly_the_same().
Referenced by nearly_the_same().
|
inlineexplicit |
|
inlineexplicit |
|
inlineexplicit |
|
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().
|
inline |
References operator==().
Referenced by is_not_empty(), is_not_zero(), and operator bool().
|
inlinedelete |
Multiplying money does not make sense, especially since we round to 2 decimal places.
|
inline |
|
inline |
References operator*().
Referenced by operator*().
|
inline |
References operator*().
Referenced by operator*().
|
inline |
References operator*().
Referenced by operator*().
|
inlinedelete |
Multiplying money does not make sense, especially since we round to 2 decimal places.
|
inline |
|
inline |
References operator*=().
Referenced by operator*=().
|
inline |
References operator*=().
Referenced by operator*=().
|
inline |
References operator*=().
Referenced by operator*=().
|
inline |
|
inline |
|
inline |
References operator+().
Referenced by operator+().
|
inline |
References operator+().
Referenced by operator+().
|
inline |
References operator+().
Referenced by operator+().
|
inline |
|
inline |
|
inline |
References operator+=().
Referenced by operator+=().
|
inline |
References operator+=().
Referenced by operator+=().
|
inline |
References operator+=().
Referenced by operator+=().
|
inline |
|
inline |
|
inline |
|
inline |
References operator-().
Referenced by operator-().
|
inline |
References operator-().
Referenced by operator-().
|
inline |
References operator-().
Referenced by operator-().
|
inline |
|
inline |
|
inline |
References operator-=().
Referenced by operator-=().
|
inline |
References operator-=().
Referenced by operator-=().
|
inline |
References operator-=().
Referenced by operator-=().
|
inlinedelete |
Dividing money does not make sense, especially since we round to 2 decimal places.
|
inline |
|
inline |
References operator/().
Referenced by operator/().
|
inline |
References operator/().
Referenced by operator/().
|
inline |
References operator/().
Referenced by operator/().
|
inlinedelete |
Dividing money does not make sense, especially since we round to 2 decimal places.
|
inline |
|
inline |
References operator/=().
Referenced by operator/=().
|
inline |
References operator/=().
Referenced by operator/=().
|
inline |
References operator/=().
Referenced by operator/=().
|
inline |
References money.
Referenced by is_less_than_zero(), and operator<=().
|
inline |
References operator<(), and operator==().
Referenced by is_zero_or_less().
|
inline |
References nearly_the_same().
Referenced by empty(), is_empty(), is_zero(), operator!=(), operator<=(), and operator>=().
|
inline |
References money.
Referenced by is_more_than_zero(), and operator>=().
|
inline |
References operator==(), and operator>().
Referenced by is_zero_or_more().
|
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().
|
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().
|
inline |
Convert the currency to a built-in type.
References round_to_cents().
Referenced by operator double(), and operator long double().
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<<().
|
mutableprotected |
Referenced by c_str().
|
protected |
Referenced by Money(), nearly_the_same(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), operator<(), operator>(), and round_to_cents().