userver: decimal64::Decimal< Prec, RoundPolicy_ > Class Template Reference
Loading...
Searching...
No Matches
decimal64::Decimal< Prec, RoundPolicy_ > Class Template Reference

Fixed-point decimal data type for use in deterministic calculations, oftentimes involving money. More...

#include <userver/decimal64/decimal64.hpp>

Public Types

using RoundPolicy = RoundPolicy_
 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>
Decimaloperator= (Decimal< Prec2, RoundPolicy > rhs)
 Assignment from another Decimal
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimaloperator= (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 Decimaloperator+= (Decimal< Prec2, RoundPolicy > rhs)
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimaloperator+= (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 Decimaloperator-= (Decimal< Prec2, RoundPolicy > rhs)
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimaloperator-= (Int rhs)
 
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal operator* (Int rhs) const
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimaloperator*= (Int rhs)
 
template<int Prec2>
constexpr Decimal operator* (Decimal< Prec2, RoundPolicy > rhs) const
 
template<int Prec2>
constexpr Decimaloperator*= (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 Decimaloperator/= (Int rhs)
 
template<int Prec2>
constexpr Decimal operator/ (Decimal< Prec2, RoundPolicy > rhs) const
 
template<int Prec2>
constexpr Decimaloperator/= (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.
 

Friends

template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator+ (Int lhs, Decimal rhs)
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator- (Int lhs, Decimal rhs)
 
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator* (Int lhs, Decimal rhs)
 
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal operator/ (Int lhs, Decimal rhs)
 
template<typename T , int OldPrec, typename OldRound >
constexpr T decimal_cast (Decimal< OldPrec, OldRound > arg)
 Cast one Decimal to another Decimal type.
 

Detailed Description

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
PrecThe number of fractional digits
RoundPolicySpecifies 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:

// create a single alias instead of specifying Decimal everywhere
std::vector<std::string> cart = ...;
Money sum{0};
for (const std::string& cost_string : cart) {
// or use FromStringPermissive to enable rounding
sum += Money{cost_string};
}
return ToString(sum);

Definition at line 446 of file decimal64.hpp.

Member Typedef Documentation

◆ RoundPolicy

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
using decimal64::Decimal< Prec, RoundPolicy_ >::RoundPolicy = RoundPolicy_

Specifies how to round in lossy operations.

Definition at line 452 of file decimal64.hpp.

Constructor & Destructor Documentation

◆ Decimal() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr decimal64::Decimal< Prec, RoundPolicy_ >::Decimal ( Int  value)
inlineexplicitconstexpr

Convert from an integer.

Definition at line 462 of file decimal64.hpp.

◆ Decimal() [2/2]

template<int Prec, typename RoundPolicy >
constexpr decimal64::Decimal< Prec, RoundPolicy >::Decimal ( std::string_view  value)
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).

Exceptions
decimal64::ParseErroron invalid input
See also
FromStringPermissive

Definition at line 1254 of file decimal64.hpp.

Member Function Documentation

◆ Abs()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::Abs ( ) const
inlineconstexpr

Returns the absolute value of the Decimal

Definition at line 723 of file decimal64.hpp.

◆ AsUnbiased()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr int64_t decimal64::Decimal< Prec, RoundPolicy_ >::AsUnbiased ( ) const
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.

See also
FromUnbiased

Definition at line 772 of file decimal64.hpp.

◆ FromBiased()

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_unbiasedThe original mantissa
original_precisionThe original precision (negated exponent)

Definition at line 532 of file decimal64.hpp.

◆ FromFloatInexact()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename T >
static constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::FromFloatInexact ( value)
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.

Warning
Prefer storing and sending Decimal as string, and performing the computations between Decimals.

Definition at line 487 of file decimal64.hpp.

◆ FromStringPermissive()

template<int Prec, typename RoundPolicy >
constexpr Decimal< Prec, RoundPolicy > decimal64::Decimal< Prec, RoundPolicy >::FromStringPermissive ( std::string_view  input)
staticconstexpr

Convert from a string, allowing rounding, spaces and boundary dot.

In addition to the Decimal(str) constructor, allows:

  • rounding (as per RoundPolicy), e.g. "12.3456789" with Prec == 2
  • space characters, e.g. " \t42 \n"
  • leading and trailing dot, e.g. "5." and ".5"
Exceptions
decimal64::ParseErroron invalid input
See also
Decimal(std::string_view)

Definition at line 1267 of file decimal64.hpp.

◆ FromUnbiased()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
static constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::FromUnbiased ( int64_t  value)
inlinestaticconstexprnoexcept

Reconstruct from the internal representation, as acquired with AsUnbiased

The Decimal value will be equal to value/kDecimalFactor.

See also
AsUnbiased

Definition at line 515 of file decimal64.hpp.

◆ operator!=()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator!= ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 563 of file decimal64.hpp.

◆ operator*() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator* ( Decimal< Prec2, RoundPolicy rhs) const
inlineconstexpr

Definition at line 680 of file decimal64.hpp.

◆ operator*() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator* ( Int  rhs) const
inlineconstexpr

Definition at line 659 of file decimal64.hpp.

◆ operator*=() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator*= ( Decimal< Prec2, RoundPolicy rhs)
inlineconstexpr

Definition at line 686 of file decimal64.hpp.

◆ operator*=() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator*= ( Int  rhs)
inlineconstexpr

Definition at line 674 of file decimal64.hpp.

◆ operator+() [1/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator+ ( ) const
inlineconstexpr

Definition at line 573 of file decimal64.hpp.

◆ operator+() [2/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr auto decimal64::Decimal< Prec, RoundPolicy_ >::operator+ ( Decimal< Prec2, RoundPolicy rhs) const
inlineconstexpr

Definition at line 581 of file decimal64.hpp.

◆ operator+() [3/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator+ ( Int  rhs) const
inlineconstexpr

Definition at line 596 of file decimal64.hpp.

◆ operator+=() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator+= ( Decimal< Prec2, RoundPolicy rhs)
inlineconstexpr

Definition at line 606 of file decimal64.hpp.

◆ operator+=() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator+= ( Int  rhs)
inlineconstexpr

Definition at line 614 of file decimal64.hpp.

◆ operator-() [1/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator- ( ) const
inlineconstexpr

Definition at line 575 of file decimal64.hpp.

◆ operator-() [2/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr auto decimal64::Decimal< Prec, RoundPolicy_ >::operator- ( Decimal< Prec2, RoundPolicy rhs) const
inlineconstexpr

Definition at line 620 of file decimal64.hpp.

◆ operator-() [3/3]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator- ( Int  rhs) const
inlineconstexpr

Definition at line 635 of file decimal64.hpp.

◆ operator-=() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator-= ( Decimal< Prec2, RoundPolicy rhs)
inlineconstexpr

Definition at line 645 of file decimal64.hpp.

◆ operator-=() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator-= ( Int  rhs)
inlineconstexpr

Definition at line 653 of file decimal64.hpp.

◆ operator/() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator/ ( Decimal< Prec2, RoundPolicy rhs) const
inlineconstexpr

Definition at line 708 of file decimal64.hpp.

◆ operator/() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::operator/ ( Int  rhs) const
inlineconstexpr

Definition at line 692 of file decimal64.hpp.

◆ operator/=() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator/= ( Decimal< Prec2, RoundPolicy rhs)
inlineconstexpr

Definition at line 714 of file decimal64.hpp.

◆ operator/=() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator/= ( Int  rhs)
inlineconstexpr

Definition at line 702 of file decimal64.hpp.

◆ operator<()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator< ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 565 of file decimal64.hpp.

◆ operator<=()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator<= ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 567 of file decimal64.hpp.

◆ operator=() [1/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2>
Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator= ( Decimal< Prec2, RoundPolicy rhs)
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 549 of file decimal64.hpp.

◆ operator=() [2/2]

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal & decimal64::Decimal< Prec, RoundPolicy_ >::operator= ( Int  rhs)
inlineconstexpr

Assignment from an integer.

Definition at line 556 of file decimal64.hpp.

◆ operator==()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator== ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 561 of file decimal64.hpp.

◆ operator>()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator> ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 569 of file decimal64.hpp.

◆ operator>=()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr bool decimal64::Decimal< Prec, RoundPolicy_ >::operator>= ( Decimal< Prec, RoundPolicy_ >  rhs) const
inlineconstexpr

Definition at line 571 of file decimal64.hpp.

◆ RoundToMultipleOf()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr Decimal decimal64::Decimal< Prec, RoundPolicy_ >::RoundToMultipleOf ( Decimal< Prec, RoundPolicy_ >  base) const
inlineconstexpr

Rounds this to the nearest multiple of base according to RoundPolicy

Definition at line 726 of file decimal64.hpp.

◆ Sign()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr int decimal64::Decimal< Prec, RoundPolicy_ >::Sign ( ) const
inlineconstexpr

Returns one of {-1, 0, +1}, depending on the sign of the Decimal

Definition at line 720 of file decimal64.hpp.

◆ ToDoubleInexact()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr double decimal64::Decimal< Prec, RoundPolicy_ >::ToDoubleInexact ( ) const
inlineconstexpr

Returns the value converted to double

Warning
Operations with double, and even the returned value, is inexact. Prefer storing and sending Decimal as string, and performing the computations between Decimals.
See also
FromFloatInexact

Definition at line 743 of file decimal64.hpp.

◆ ToInteger()

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr int64_t decimal64::Decimal< Prec, RoundPolicy_ >::ToInteger ( ) const
inlineconstexpr

Returns the value rounded to integer using the active rounding policy.

Definition at line 732 of file decimal64.hpp.

Friends And Related Symbol Documentation

◆ Decimal

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<int Prec2, typename RoundPolicy2 >
friend class Decimal
friend

Definition at line 793 of file decimal64.hpp.

◆ decimal_cast

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:

using Money = decimal64::Decimal<4>;
Money cost = ...;
auto discount = decimal64::decimal_cast<Discount>(cost) * Discount{"0.05"};

Definition at line 829 of file decimal64.hpp.

◆ operator*

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator* ( Int  lhs,
Decimal< Prec, RoundPolicy_ >  rhs 
)
friend

Definition at line 669 of file decimal64.hpp.

◆ operator+

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator+ ( Int  lhs,
Decimal< Prec, RoundPolicy_ >  rhs 
)
friend

Definition at line 601 of file decimal64.hpp.

◆ operator-

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , impl::EnableIfInt< Int > = 0>
constexpr Decimal operator- ( Int  lhs,
Decimal< Prec, RoundPolicy_ >  rhs 
)
friend

Definition at line 640 of file decimal64.hpp.

◆ operator/

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
template<typename Int , typename = impl::EnableIfInt<Int>>
constexpr Decimal operator/ ( Int  lhs,
Decimal< Prec, RoundPolicy_ >  rhs 
)
friend

Definition at line 697 of file decimal64.hpp.

Member Data Documentation

◆ kDecimalFactor

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr int64_t decimal64::Decimal< Prec, RoundPolicy_ >::kDecimalFactor = kPow10<Prec>
staticconstexpr

The denominator of the decimal fraction.

Definition at line 455 of file decimal64.hpp.

◆ kDecimalPoints

template<int Prec, typename RoundPolicy_ = DefRoundPolicy>
constexpr int decimal64::Decimal< Prec, RoundPolicy_ >::kDecimalPoints = Prec
staticconstexpr

The number of fractional digits.

Definition at line 449 of file decimal64.hpp.


The documentation for this class was generated from the following file: