Common functions to work with integers and doubles. More...
Functions | |
std::string | approximateSize (const uint64_t bytes) |
Returns a simple easy-to-read text string representing the number of bytes. More... | |
std::string | format (const double &d) |
Format a double into a simple string. Will truncate trailing zeros. More... | |
std::string | format (const double &d, const int decimals) |
Format a double into a simple string. Will truncate, pad, or round as necessary to the specified number of decimal places. More... | |
std::string | format (const int &i) |
Format an integer into a simple string. More... | |
bool | nearlyTheSame (const double a, const double b) |
Determine if the two values are nearly the same. More... | |
bool | parse (const std::string &str, double &d, double &fraction) |
Parse a text string and get back the numeric value as a double. More... | |
bool | parse (const std::string &str, double &d) |
Parse a text string and get back the value as a double. More... | |
bool | parse (const std::string &str, int &i) |
Parse a text string and get back the value as an integer. This will round (not truncate) fractional values. More... | |
int | random (const int minimum, const int maximum) |
Generate a random number. More... | |
int | random (const int maximum) |
Similar to random( const int, const int ), but the minimum is set to zero. More... | |
double | random (const double minimum, const double maximum=1.0) |
Generate a random number. More... | |
std::string | reformatDoubleStr (const std::string &str) |
If the input string is a valid double, reformat the string. Make no change if the string is not a valid double. More... | |
std::string | reformatIntegerStr (const std::string &str) |
If the input string is a valid integer, reformat the string. Make no change if the string is not a valid integer. More... | |
std::string | reformatStr (const std::string &str) |
If the input string is a valid number, reformat the string. More... | |
bool | validate (const std::string &str) |
Determine if the text is a valid number. Assumes "double". More... | |
bool | validateDouble (const std::string &str) |
Determine if the text is a valid double. More... | |
bool | validateInteger (const std::string &str) |
Determine if the text is a valid integer. More... | |
double | round_to_2_decimals (double value) |
Round to 2 decimal places. More... | |
long double | round_to_2_decimals (long double value) |
Round to 2 decimal places. More... | |
Common functions to work with integers and doubles.
std::string Lox::Numbers::approximateSize | ( | const uint64_t | bytes | ) |
Returns a simple easy-to-read text string representing the number of bytes.
For example, 1048576 bytes is formatted as "1.00 MiB".
std::string Lox::Numbers::format | ( | const double & | d | ) |
Format a double into a simple string. Will truncate trailing zeros.
Referenced by Lox::Temperature::celsiusString(), Lox::Temperature::fahrenheitString(), reformatDoubleStr(), reformatIntegerStr(), Lox::Prices::reformatStr(), and Lox::timeFromString().
std::string Lox::Numbers::format | ( | const double & | d, |
const int | decimals | ||
) |
Format a double into a simple string. Will truncate, pad, or round as necessary to the specified number of decimal places.
std::string Lox::Numbers::format | ( | const int & | i | ) |
Format an integer into a simple string.
bool Lox::Numbers::nearlyTheSame | ( | const double | a, |
const double | b | ||
) |
Determine if the two values are nearly the same.
a
and b
is smaller than 0.01 Referenced by Lox::Measurement::internalSanityCheck(), Lox::Measurement::mmToStr(), and Lox::Measurement::parse().
bool Lox::Numbers::parse | ( | const std::string & | str, |
double & | d, | ||
double & | fraction | ||
) |
Parse a text string and get back the numeric value as a double.
places the value "3" into "d", and the value "0.14159" into "fraction". To get back the original value, add both values together.
Format must be one of the following:
# .# #.#
Additional text may appear after the number, in which case it is ignored.
Referenced by Lox::Prices::parse(), parse(), reformatDoubleStr(), reformatIntegerStr(), Lox::Prices::reformatStr(), Lox::Measurement::strToMM(), Lox::Prices::validate(), Lox::Measurement::validate(), validateDouble(), and validateInteger().
bool Lox::Numbers::parse | ( | const std::string & | str, |
double & | d | ||
) |
Parse a text string and get back the value as a double.
References parse().
bool Lox::Numbers::parse | ( | const std::string & | str, |
int & | i | ||
) |
Parse a text string and get back the value as an integer. This will round (not truncate) fractional values.
The value returned will be rounded to the nearest integer. For example, 2.7 will be returned as 3.
References parse().
int Lox::Numbers::random | ( | const int | minimum, |
const int | maximum | ||
) |
Generate a random number.
The range is inclusive. E.g., for a normal 6-sided dice, you'd call random( 1, 6 ). The first time this method is called, the random number generator is seeded using std::random_device.
Referenced by Lox::Colours::getRandomColour(), Lox::Colours::getRandomGradient(), and random().
int Lox::Numbers::random | ( | const int | maximum | ) |
Similar to random( const int, const int ), but the minimum is set to zero.
References random().
double Lox::Numbers::random | ( | const double | minimum, |
const double | maximum = 1.0 |
||
) |
Generate a random number.
Unlike the integer random number generator, this one produces values uniformly distributed on the interval [min, max), meaning the maximum value is outside of the range. The first time this method is called, the random number generator is seeded using std::random_device.
std::string Lox::Numbers::reformatDoubleStr | ( | const std::string & | str | ) |
If the input string is a valid double, reformat the string. Make no change if the string is not a valid double.
References format(), and parse().
Referenced by reformatStr().
std::string Lox::Numbers::reformatIntegerStr | ( | const std::string & | str | ) |
If the input string is a valid integer, reformat the string. Make no change if the string is not a valid integer.
References format(), and parse().
Referenced by reformatStr().
std::string Lox::Numbers::reformatStr | ( | const std::string & | str | ) |
If the input string is a valid number, reformat the string.
Make no change if the string is not a valid number. Makes a guess as to whether the value is a double or an integer.
References reformatDoubleStr(), and reformatIntegerStr().
Referenced by Lox::Measurement::internalSanityCheck().
double Lox::Numbers::round_to_2_decimals | ( | double | value | ) |
Round to 2 decimal places.
Referenced by Lox::Measurement::MM_SQUARE_TO_FEET_SQUARE_ROUNDED(), Lox::Measurement::MM_TO_FT_ROUNDED(), and Lox::Money::round_to_cents().
long double Lox::Numbers::round_to_2_decimals | ( | long double | value | ) |
Round to 2 decimal places.
bool Lox::Numbers::validate | ( | const std::string & | str | ) |
Determine if the text is a valid number. Assumes "double".
References validateDouble().
bool Lox::Numbers::validateDouble | ( | const std::string & | str | ) |
Determine if the text is a valid double.
References parse().
Referenced by validate().
bool Lox::Numbers::validateInteger | ( | const std::string & | str | ) |
Determine if the text is a valid integer.
References parse().