#include <userver/decimal64/decimal64.hpp>
Fixed-point decimal data type for use in deterministic calculations, oftentimes involving money.
| 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:
Definition at line 454 of file decimal64.hpp.
Public Types | |
| using | RoundPolicy = TRoundPolicy |
| Specifies how to round in lossy operations. | |
Public Member Functions | |
| 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 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<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. | |
Static Public Member Functions | |
| 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. | |
Static Public Attributes | |
| static constexpr int | kDecimalPoints = Prec |
| The number of fractional digits. | |
| static constexpr int64_t | kDecimalFactor = kPow10<Prec> |
| The denominator of the decimal fraction. | |
| using decimal64::Decimal< Prec, TRoundPolicy >::RoundPolicy = TRoundPolicy |
Specifies how to round in lossy operations.
Definition at line 460 of file decimal64.hpp.
|
inlineexplicitconstexpr |
Convert from an integer.
Definition at line 470 of file decimal64.hpp.
|
explicitconstexpr |
Convert from a string.
The string must match the following regexp exactly:
[+-]?\d+(\.\d+)?
No extra characters, including spaces, are allowed. Extra leading and trailing zeros (within Prec) are discarded. Input containing more fractional digits that Prec is not allowed (no implicit rounding). Exponential format (e.g., "1.23e4", "5E-2") is not supported by this constructor.
| decimal64::ParseError | on invalid input |
Definition at line 1467 of file decimal64.hpp.
|
inlineconstexpr |
Returns the absolute value of the Decimal
Definition at line 737 of file decimal64.hpp.
|
inlineconstexpr |
Retrieve the internal representation.
The internal representation of Decimal is real_value * kDecimalFactor. Use for storing the value of Decimal efficiently when Prec is guaranteed not to change.
Definition at line 785 of file decimal64.hpp.
|
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
| original_unbiased | The original mantissa |
| original_precision | The original precision (negated exponent) |
Definition at line 547 of file decimal64.hpp.
|
inlinestaticconstexpr |
Lossy conversion from a floating-point number.
To somewhat resist the accumulated error, the number is always rounded to the nearest Decimal, regardless of RoundPolicy.
Decimal as string, and performing the computations between Decimals. Definition at line 498 of file decimal64.hpp.
|
staticconstexpr |
Convert from a string, allowing rounding, spaces and boundary dot.
In addition to the Decimal(str) constructor, allows:
RoundPolicy), e.g. "12.3456789" with Prec == 2| decimal64::ParseError | on invalid input |
Definition at line 1477 of file decimal64.hpp.
|
inlinestaticconstexprnoexcept |
Reconstruct from the internal representation, as acquired with AsUnbiased
The Decimal value will be equal to value/kDecimalFactor.
Definition at line 530 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 696 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 676 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 701 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 690 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 590 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 600 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 615 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 625 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 632 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 592 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 638 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 653 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 663 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 670 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 723 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 707 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 728 of file decimal64.hpp.
|
inlineconstexpr |
Definition at line 717 of file decimal64.hpp.
|
inline |
Assignment from another Decimal
The assignment is allowed as long as RoundPolicy is the same. Rounding will be performed according to RoundPolicy if necessary.
Definition at line 562 of file decimal64.hpp.
|
inlineconstexpr |
Assignment from an integer.
Definition at line 569 of file decimal64.hpp.
|
inlineconstexpr |
Rounds this to the nearest multiple of base according to RoundPolicy
Definition at line 740 of file decimal64.hpp.
|
inlineconstexpr |
Returns one of {-1, 0, +1}, depending on the sign of the Decimal
Definition at line 734 of file decimal64.hpp.
|
inlineconstexpr |
Returns the value converted to double
double, and even the returned value, is inexact. Prefer storing and sending Decimal as string, and performing the computations between Decimals.Definition at line 757 of file decimal64.hpp.
|
inlineconstexpr |
Returns the value rounded to integer using the active rounding policy.
Definition at line 748 of file decimal64.hpp.
|
friend |
Definition at line 804 of file decimal64.hpp.
|
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:
Definition at line 840 of file decimal64.hpp.
|
friend |
Definition at line 685 of file decimal64.hpp.
|
friend |
Definition at line 620 of file decimal64.hpp.
|
friend |
Definition at line 658 of file decimal64.hpp.
|
friend |
Definition at line 712 of file decimal64.hpp.
|
staticconstexpr |
The denominator of the decimal fraction.
Definition at line 463 of file decimal64.hpp.
|
staticconstexpr |
The number of fractional digits.
Definition at line 457 of file decimal64.hpp.