template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
class decimal64::Decimal< Prec, RoundPolicy_ >
Fixed-point decimal data type for use in deterministic calculations, oftentimes involving money.
- Template Parameters
-
Prec | The number of fractional digits |
RoundPolicy | Specifies how to round in lossy operations |
Decimal is internally represented as int64_t
. It means that it can be passed around by value. It also means that operations with huge numbers can overflow and trap. For example, with Prec == 6
, the maximum representable number is about 10 trillion.
Decimal should be serialized and stored as a string, NOT as double
. Use Decimal{str}
constructor (or Decimal::FromStringPermissive
if rounding is allowed) to read a Decimal
, and ToString(dec)
(or ToStringTrailingZeros(dec)
/ToStringFixed<N>(dec)
) to write a Decimal
.
Use arithmetic with caution! Multiplication and division operations involve rounding. You may want to cast to Decimal
with another Prec
or RoundPolicy
beforehand. For that purpose you can use decimal64::decimal_cast<NewDec>(dec)
.
Usage example:
std::vector<std::string> cart = ...;
Money sum{0};
for (const std::string& cost_string : cart) {
sum += Money{cost_string};
}
Definition at line 436 of file decimal64.hpp.
|
constexpr | Decimal () noexcept=default |
| Zero by default.
|
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr | Decimal (Int value) |
| Convert from an integer.
|
|
constexpr | Decimal (std::string_view value) |
| Convert from a string.
|
|
template<int Prec2> |
Decimal & | operator= (Decimal< Prec2, RoundPolicy > rhs) |
| Assignment from another Decimal
|
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal & | operator= (Int rhs) |
| Assignment from an integer.
|
|
constexpr bool | operator== (Decimal rhs) const |
|
constexpr bool | operator!= (Decimal rhs) const |
|
constexpr bool | operator< (Decimal rhs) const |
|
constexpr bool | operator<= (Decimal rhs) const |
|
constexpr bool | operator> (Decimal rhs) const |
|
constexpr bool | operator>= (Decimal rhs) const |
|
constexpr Decimal | operator+ () const |
|
constexpr Decimal | operator- () const |
|
template<int Prec2> |
constexpr auto | operator+ (Decimal< Prec2, RoundPolicy > rhs) const |
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal | operator+ (Int rhs) const |
|
template<int Prec2> |
constexpr Decimal & | operator+= (Decimal< Prec2, RoundPolicy > rhs) |
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal & | operator+= (Int rhs) |
|
template<int Prec2> |
constexpr auto | operator- (Decimal< Prec2, RoundPolicy > rhs) const |
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal | operator- (Int rhs) const |
|
template<int Prec2> |
constexpr Decimal & | operator-= (Decimal< Prec2, RoundPolicy > rhs) |
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal & | operator-= (Int rhs) |
|
template<typename Int , typename = impl::EnableIfInt<Int>> |
constexpr Decimal | operator* (Int rhs) const |
|
template<typename Int , impl::EnableIfInt< Int > = 0> |
constexpr Decimal & | operator*= (Int rhs) |
|
template<int Prec2> |
constexpr Decimal | operator* (Decimal< Prec2, RoundPolicy > rhs) const |
|
template<int Prec2> |
constexpr Decimal & | operator*= (Decimal< Prec2, RoundPolicy > rhs) |
|
template<typename Int , typename = impl::EnableIfInt<Int>> |
constexpr Decimal | operator/ (Int rhs) const |
|
template<typename Int , typename = impl::EnableIfInt<Int>> |
constexpr Decimal & | operator/= (Int rhs) |
|
template<int Prec2> |
constexpr Decimal | operator/ (Decimal< Prec2, RoundPolicy > rhs) const |
|
template<int Prec2> |
constexpr Decimal & | operator/= (Decimal< Prec2, RoundPolicy > rhs) |
|
constexpr int | Sign () const |
| Returns one of {-1, 0, +1}, depending on the sign of the Decimal
|
|
constexpr Decimal | Abs () const |
| Returns the absolute value of the Decimal
|
|
constexpr Decimal | RoundToMultipleOf (Decimal base) const |
| Rounds this to the nearest multiple of base according to RoundPolicy
|
|
constexpr int64_t | ToInteger () const |
| Returns the value rounded to integer using the active rounding policy.
|
|
constexpr double | ToDoubleInexact () const |
| Returns the value converted to double
|
|
constexpr int64_t | AsUnbiased () const |
| Retrieve the internal representation.
|
|
|
template<typename T > |
static constexpr Decimal | FromFloatInexact (T value) |
| Lossy conversion from a floating-point number.
|
|
static constexpr Decimal | FromStringPermissive (std::string_view input) |
| Convert from a string, allowing rounding, spaces and boundary dot.
|
|
static constexpr Decimal | FromUnbiased (int64_t value) noexcept |
| Reconstruct from the internal representation, as acquired with AsUnbiased
|
|
static constexpr Decimal | FromBiased (int64_t original_unbiased, int original_precision) |
| Convert from original_unbiased / 10^original_precision , rounding according to RoundPolicy if necessary.
|
|
template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
static constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::FromBiased |
( |
int64_t | original_unbiased, |
|
|
int | original_precision ) |
|
inlinestaticconstexpr |
Convert from original_unbiased / 10^original_precision
, rounding according to RoundPolicy
if necessary.
Usage examples:
Decimal<4>::FromBiased(123, 6) -> 0.0001
Decimal<4>::FromBiased(123, 2) -> 1.23
Decimal<4>::FromBiased(123, -1) -> 1230
- Parameters
-
original_unbiased | The original mantissa |
original_precision | The original precision (negated exponent) |
Definition at line 524 of file decimal64.hpp.
template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename T , int OldPrec, typename OldRound >
constexpr T decimal_cast |
( |
Decimal< OldPrec, OldRound > | arg | ) |
|
|
friend |
Cast one Decimal
to another Decimal
type.
When casting to a Decimal
with a lower Prec
, rounding is performed according to the new RoundPolicy
.
Usage example:
Money cost = ...;
auto discount = decimal64::decimal_cast<Discount>(cost) * Discount{"0.05"};
Definition at line 813 of file decimal64.hpp.