Binance C++ Connector 2.0.0
Official C++ connectors for the Binance APIs
Loading...
Searching...
No Matches
binance::common::Decimal Class Reference

Exact decimal number, stored as its canonical plain-notation string. More...

#include <decimal.hpp>

Public Member Functions

 Decimal ()=default
 Decimal (std::string_view text)
 Parses "123", "-0.001", "1.5e-7", …; throws std::invalid_argument otherwise.
 Decimal (const char *text)
 Throws std::invalid_argument for a null pointer.
 Decimal (const std::string &text)
 Decimal (double value)
 Shortest representation that round-trips the double (0.1 → "0.1").
template<std::same_as< float > T>
 Decimal (T)=delete
 Rejected: a float widens to a double that is not the value it was written as (0.1f → 0.100000001490116).
template<std::integral T>
requires (!detail::BoolOrCharacter<T>)
 Decimal (T value)
template<detail::BoolOrCharacter T>
 Decimal (T)=delete
 bool and character types are rejected.
 Decimal (const Decimal &)=default
Decimal & operator= (const Decimal &)=default
 Decimal (Decimal &&other) noexcept
 A moved-from Decimal is zero (still canonical).
Decimal & operator= (Decimal &&other) noexcept
 ~Decimal ()=default
const std::string & str () const noexcept
double to_double () const
 The nearest double; values beyond its range become ±HUGE_VAL or ±0.0.

Friends

bool operator== (const Decimal &a, const Decimal &b) noexcept=default
 Numeric comparison. Equality is exact because the representation is canonical.
std::strong_ordering operator<=> (const Decimal &a, const Decimal &b) noexcept

Detailed Description

Exact decimal number, stored as its canonical plain-notation string.

Canonical form: optional -, digits, optional . and digits. Leading zeros of the integer part, trailing zeros of the fraction and -0 are normalised away ("0010.500" → "10.5").

Exactness is guaranteed for text and integer inputs only. A double already carries binary rounding, so Decimal(double) keeps the shortest text that round-trips it, not the exact value the double was meant to hold.

Constructor & Destructor Documentation

◆ Decimal() [1/10]

binance::common::Decimal::Decimal ( )
default

◆ Decimal() [2/10]

binance::common::Decimal::Decimal ( std::string_view text)
explicit

Parses "123", "-0.001", "1.5e-7", …; throws std::invalid_argument otherwise.

◆ Decimal() [3/10]

binance::common::Decimal::Decimal ( const char * text)
explicit

Throws std::invalid_argument for a null pointer.

◆ Decimal() [4/10]

binance::common::Decimal::Decimal ( const std::string & text)
inlineexplicit

◆ Decimal() [5/10]

binance::common::Decimal::Decimal ( double value)
explicit

Shortest representation that round-trips the double (0.1 → "0.1").

Throws std::invalid_argument for NaN / infinity.

◆ Decimal() [6/10]

template<std::same_as< float > T>
binance::common::Decimal::Decimal ( T )
delete

Rejected: a float widens to a double that is not the value it was written as (0.1f → 0.100000001490116).

Pass a string, or cast to double explicitly.

◆ Decimal() [7/10]

template<std::integral T>
requires (!detail::BoolOrCharacter<T>)
binance::common::Decimal::Decimal ( T value)
inlineexplicit

◆ Decimal() [8/10]

template<detail::BoolOrCharacter T>
binance::common::Decimal::Decimal ( T )
delete

bool and character types are rejected.

◆ Decimal() [9/10]

binance::common::Decimal::Decimal ( const Decimal & )
default

◆ Decimal() [10/10]

binance::common::Decimal::Decimal ( Decimal && other)
inlinenoexcept

A moved-from Decimal is zero (still canonical).

◆ ~Decimal()

binance::common::Decimal::~Decimal ( )
default

Member Function Documentation

◆ operator=() [1/2]

Decimal & binance::common::Decimal::operator= ( const Decimal & )
default

◆ operator=() [2/2]

Decimal & binance::common::Decimal::operator= ( Decimal && other)
inlinenoexcept

◆ str()

const std::string & binance::common::Decimal::str ( ) const
inlinenodiscardnoexcept

◆ to_double()

double binance::common::Decimal::to_double ( ) const
nodiscard

The nearest double; values beyond its range become ±HUGE_VAL or ±0.0.

◆ operator==

bool operator== ( const Decimal & a,
const Decimal & b )
friend

Numeric comparison. Equality is exact because the representation is canonical.

◆ operator<=>

std::strong_ordering operator<=> ( const Decimal & a,
const Decimal & b )
friend