template<int Prec, typename TRoundPolicy = DefRoundPolicy>
class decimal64::Decimal< Prec, TRoundPolicy >
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 and exponential format are 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 449 of file decimal64.hpp.
|
|
constexpr | Decimal () noexcept=default |
| | Zero by default.
|
| template<meta::kIsInteger Int> |
| 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<meta::kIsInteger Int> |
| constexpr Decimal & | operator= (Int rhs) |
| | Assignment from an integer.
|
|
constexpr auto | operator<=> (const Decimal &rhs) const =default |
| constexpr Decimal | operator+ () const |
| constexpr Decimal | operator- () const |
| template<int Prec2> |
| constexpr auto | operator+ (Decimal< Prec2, RoundPolicy > rhs) const |
| template<meta::kIsInteger Int> |
| constexpr Decimal | operator+ (Int rhs) const |
| template<int Prec2> |
| constexpr Decimal & | operator+= (Decimal< Prec2, RoundPolicy > rhs) |
| template<meta::kIsInteger Int> |
| constexpr Decimal & | operator+= (Int rhs) |
| template<int Prec2> |
| constexpr auto | operator- (Decimal< Prec2, RoundPolicy > rhs) const |
| template<meta::kIsInteger Int> |
| constexpr Decimal | operator- (Int rhs) const |
| template<int Prec2> |
| constexpr Decimal & | operator-= (Decimal< Prec2, RoundPolicy > rhs) |
| template<meta::kIsInteger Int> |
| constexpr Decimal & | operator-= (Int rhs) |
| template<meta::kIsInteger Int> |
| constexpr Decimal | operator* (Int rhs) const |
| template<meta::kIsInteger 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) |
| template<meta::kIsInteger Int> |
| constexpr Decimal | operator/ (Int rhs) const |
| template<meta::kIsInteger 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 TRoundPolicy = DefRoundPolicy>
| constexpr Decimal decimal64::Decimal< Prec, TRoundPolicy >::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 542 of file decimal64.hpp.