userver: decimal64 Namespace Reference
Loading...
Searching...
No Matches
decimal64 Namespace Reference

Detailed Description

Fixed-point decimal data type and related functions.

Classes

class  CeilingRoundPolicy
 Round towards +infinity. More...
 
class  Decimal
 Fixed-point decimal data type for use in deterministic calculations, oftentimes involving money. More...
 
class  DecimalError
 The base class for Decimal-related exceptions. More...
 
class  DefRoundPolicy
 Default rounding. Fast, rounds to nearest. More...
 
class  DivisionByZeroError
 Thrown on division by zero in Decimal arithmetic. More...
 
class  FloorRoundPolicy
 Round towards -infinity. More...
 
struct  FormatOptions
 Parameters for flexible decimal formatting. More...
 
class  HalfDownRoundPolicy
 Round to nearest, 0.5 towards zero. More...
 
class  HalfEvenRoundPolicy
 Round to nearest, 0.5 towards number with even last digit. More...
 
class  HalfUpRoundPolicy
 Round to nearest, 0.5 away from zero. More...
 
class  OutOfBoundsError
 Thrown on overflow in Decimal arithmetic. More...
 
class  ParseError
 Thrown on all errors related to parsing Decimal from string. More...
 
class  RoundDownRoundPolicy
 Round towards zero. The fastest rounding. More...
 
class  RoundUpRoundPolicy
 Round away from zero. More...
 

Functions

constexpr int64_t Pow10 (int exp)
 A fast, constexpr-friendly power of 10.
 
template<typename T , int OldPrec, typename OldRound >
constexpr T decimal_cast (Decimal< OldPrec, OldRound > arg)
 Cast one Decimal to another Decimal type.
 
template<int Prec, typename RoundPolicy >
std::string ToString (Decimal< Prec, RoundPolicy > dec)
 Converts Decimal to a string.
 
template<int Prec, typename RoundPolicy >
std::string ToString (const Decimal< Prec, RoundPolicy > &dec, const FormatOptions &format_options)
 Converts Decimal to a string.
 
template<int Prec, typename RoundPolicy >
std::string ToStringTrailingZeros (Decimal< Prec, RoundPolicy > dec)
 Converts Decimal to a string, writing exactly Prec decimal digits.
 
template<int NewPrec, int Prec, typename RoundPolicy >
std::string ToStringFixed (Decimal< Prec, RoundPolicy > dec)
 Converts Decimal to a string with exactly NewPrec decimal digits.
 
template<typename CharT , typename Traits , int Prec, typename RoundPolicy >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, Decimal< Prec, RoundPolicy > &d)
 Parses a Decimal from the istream
 
template<typename CharT , typename Traits , int Prec, typename RoundPolicy >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const Decimal< Prec, RoundPolicy > &d)
 Writes the Decimal to the ostream
 
template<int Prec, typename RoundPolicy >
logging::LogHelperoperator<< (logging::LogHelper &lh, const Decimal< Prec, RoundPolicy > &d)
 Writes the Decimal to the logger.
 
template<int Prec, typename RoundPolicy , typename Value >
std::enable_if_t< formats::common::kIsFormatValue< Value >, Decimal< Prec, RoundPolicy > > Parse (const Value &value, formats::parse::To< Decimal< Prec, RoundPolicy > >)
 Parses the Decimal from the string.
 
template<int Prec, typename RoundPolicy , typename TargetType >
TargetType Serialize (const Decimal< Prec, RoundPolicy > &object, formats::serialize::To< TargetType >)
 Serializes the Decimal to string.
 
template<int Prec, typename RoundPolicy , typename StringBuilder >
void WriteToStream (const Decimal< Prec, RoundPolicy > &object, StringBuilder &sw)
 Writes the Decimal to stream.
 
template<int Prec, typename RoundPolicy >
void PrintTo (const Decimal< Prec, RoundPolicy > &v, std::ostream *os)
 

Variables

template<int Exp>
constexpr int64_t kPow10 = Pow10(Exp)
 A guaranteed-compile-time power of 10.
 
template<typename T >
constexpr bool kIsDecimal = impl::IsDecimal<T>::value
 true if the type is an instantiation of Decimal
 

Function Documentation

◆ decimal_cast()

template<typename T , int OldPrec, typename OldRound >
constexpr T decimal64::decimal_cast ( Decimal< OldPrec, OldRound > arg)
constexpr

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 843 of file decimal64.hpp.

◆ operator<<() [1/2]

template<int Prec, typename RoundPolicy >
logging::LogHelper & decimal64::operator<< ( logging::LogHelper & lh,
const Decimal< Prec, RoundPolicy > & d )

Writes the Decimal to the logger.

See also
ToString

Definition at line 1400 of file decimal64.hpp.

◆ operator<<() [2/2]

template<typename CharT , typename Traits , int Prec, typename RoundPolicy >
std::basic_ostream< CharT, Traits > & decimal64::operator<< ( std::basic_ostream< CharT, Traits > & os,
const Decimal< Prec, RoundPolicy > & d )

Writes the Decimal to the ostream

See also
ToString

Definition at line 1371 of file decimal64.hpp.

◆ operator>>()

template<typename CharT , typename Traits , int Prec, typename RoundPolicy >
std::basic_istream< CharT, Traits > & decimal64::operator>> ( std::basic_istream< CharT, Traits > & is,
Decimal< Prec, RoundPolicy > & d )

Parses a Decimal from the istream

Acts like the Decimal(str) constructor, except that it allows junk that immediately follows the number. Sets the stream's fail bit on failure.

Usage example:

if (os >> dec) {
  // success
} else {
  // failure
}
See also
Decimal::Decimal(std::string_view)

Definition at line 1371 of file decimal64.hpp.

◆ Parse()

template<int Prec, typename RoundPolicy , typename Value >
std::enable_if_t< formats::common::kIsFormatValue< Value >, Decimal< Prec, RoundPolicy > > decimal64::Parse ( const Value & value,
formats::parse::To< Decimal< Prec, RoundPolicy > >  )

Parses the Decimal from the string.

See also
Decimal::Decimal(std::string_view)

Definition at line 1411 of file decimal64.hpp.

◆ Pow10()

constexpr int64_t decimal64::Pow10 ( int exp)
constexpr

A fast, constexpr-friendly power of 10.

Definition at line 307 of file decimal64.hpp.

◆ PrintTo()

template<int Prec, typename RoundPolicy >
void decimal64::PrintTo ( const Decimal< Prec, RoundPolicy > & v,
std::ostream * os )

Definition at line 62 of file utest.hpp.

◆ Serialize()

template<int Prec, typename RoundPolicy , typename TargetType >
TargetType decimal64::Serialize ( const Decimal< Prec, RoundPolicy > & object,
formats::serialize::To< TargetType >  )

Serializes the Decimal to string.

See also
ToString

Definition at line 1428 of file decimal64.hpp.

◆ ToString() [1/2]

template<int Prec, typename RoundPolicy >
std::string decimal64::ToString ( const Decimal< Prec, RoundPolicy > & dec,
const FormatOptions & format_options )

Converts Decimal to a string.

Usage example:

ToString(decimal64::Decimal<4>{"-1234.1234"},
         {"||", "**", "\1", "<>", {}, true}))   -> "<>1**2**3**4||1234"
ToString(decimal64::Decimal<4>{"-1234.1234"},
         {",", " ", "\3", "-", 6, true}))       -> "-1 234,123400"
See also
ToStringTrailingZeros
ToStringFixed

Definition at line 1319 of file decimal64.hpp.

◆ ToString() [2/2]

template<int Prec, typename RoundPolicy >
std::string decimal64::ToString ( Decimal< Prec, RoundPolicy > dec)

Converts Decimal to a string.

Usage example:

ToString(decimal64::Decimal<4>{"1.5"}) -> 1.5
See also
ToStringTrailingZeros
ToStringFixed

Definition at line 1303 of file decimal64.hpp.

◆ ToStringFixed()

template<int NewPrec, int Prec, typename RoundPolicy >
std::string decimal64::ToStringFixed ( Decimal< Prec, RoundPolicy > dec)

Converts Decimal to a string with exactly NewPrec decimal digits.

Usage example:

ToStringFixed<3>(decimal64::Decimal<4>{"1.5"}) -> 1.500
See also
ToString
ToStringTrailingZeros

Definition at line 1351 of file decimal64.hpp.

◆ ToStringTrailingZeros()

template<int Prec, typename RoundPolicy >
std::string decimal64::ToStringTrailingZeros ( Decimal< Prec, RoundPolicy > dec)

Converts Decimal to a string, writing exactly Prec decimal digits.

Usage example:

ToStringTrailingZeros(decimal64::Decimal<4>{"1.5"}) -> 1.5000
See also
ToString
ToStringFixed

Definition at line 1338 of file decimal64.hpp.

◆ WriteToStream()

template<int Prec, typename RoundPolicy , typename StringBuilder >
void decimal64::WriteToStream ( const Decimal< Prec, RoundPolicy > & object,
StringBuilder & sw )

Writes the Decimal to stream.

See also
ToString

Definition at line 1436 of file decimal64.hpp.

Variable Documentation

◆ kIsDecimal

template<typename T >
constexpr bool decimal64::kIsDecimal = impl::IsDecimal<T>::value
inlineconstexpr

true if the type is an instantiation of Decimal

Definition at line 827 of file decimal64.hpp.

◆ kPow10

template<int Exp>
constexpr int64_t decimal64::kPow10 = Pow10(Exp)
inlineconstexpr

A guaranteed-compile-time power of 10.

Definition at line 316 of file decimal64.hpp.