Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::SqlDynamicNumeric Struct Reference

A fixed-point decimal whose precision and scale are known only at run time. More...

#include <SqlDynamicNumeric.hpp>

Public Member Functions

constexpr bool operator== (SqlDynamicNumeric const &other) const noexcept
 
constexpr bool operator!= (SqlDynamicNumeric const &other) const noexcept
 Inequality, derived from operator==.
 
constexpr double ToDouble () const noexcept
 
std::string ToString () const
 Renders the exact decimal representation, including the trailing zeros implied by scale.
 

Static Public Member Functions

static constexpr std::optional< SqlDynamicNumeric > FromString (std::string_view text, std::uint8_t precision, std::uint8_t scale) noexcept
 

Public Attributes

std::int64_t unscaledValue = 0
 The value, scaled by 10^scale. 12.34 at scale 2 is stored as 1234.
 
std::uint8_t precision = 0
 Total number of decimal digits the source column holds.
 
std::uint8_t scale = 0
 Number of digits after the decimal point.
 

Detailed Description

A fixed-point decimal whose precision and scale are known only at run time.

This is the dynamic counterpart to SqlNumeric. Where SqlNumeric<Precision, Scale> fixes both at compile time, a value fetched into a SqlVariant learns its precision and scale from the result-set metadata, so they have to travel with the value.

The number is held exactly, as the unscaled integer value * 10^scale, and never passes through a binary floating-point type. That is the point of the type: DECIMAL(19, 4) — what MS SQL Server's money maps to — carries more significant digits than a double can represent, so reading such a column as double loses cents on large amounts.

Definition at line 239 of file SqlDynamicNumeric.hpp.

Member Function Documentation

◆ operator==()

constexpr bool Lightweight::SqlDynamicNumeric::operator== ( SqlDynamicNumeric const &  other) const
inlineconstexprnoexcept

Compares two values by mathematical magnitude, so the same number at different scales compares equal (1.50 at scale 2 equals 1.5 at scale 1).

Definition at line 252 of file SqlDynamicNumeric.hpp.

References scale, and unscaledValue.

◆ operator!=()

constexpr bool Lightweight::SqlDynamicNumeric::operator!= ( SqlDynamicNumeric const &  other) const
inlineconstexprnoexcept

Inequality, derived from operator==.

Definition at line 271 of file SqlDynamicNumeric.hpp.

◆ ToDouble()

constexpr double Lightweight::SqlDynamicNumeric::ToDouble ( ) const
inlineconstexprnoexcept

Converts to a floating-point approximation.

Note
Lossy for values needing more significant digits than a double carries; read unscaledValue together with scale when exactness matters.

Definition at line 280 of file SqlDynamicNumeric.hpp.

References scale, and unscaledValue.

◆ ToString()

std::string Lightweight::SqlDynamicNumeric::ToString ( ) const
inline

Renders the exact decimal representation, including the trailing zeros implied by scale.

Definition at line 294 of file SqlDynamicNumeric.hpp.

References scale, and unscaledValue.

Referenced by Lightweight::SqlDataBinder< SqlDynamicNumeric >::Inspect().

◆ FromString()

static constexpr std::optional< SqlDynamicNumeric > Lightweight::SqlDynamicNumeric::FromString ( std::string_view  text,
std::uint8_t  precision,
std::uint8_t  scale 
)
inlinestaticconstexprnoexcept

Parses an exact decimal literal, scaling it to scale.

Parameters
textThe decimal literal, as an ODBC driver renders a DECIMAL/NUMERIC column.
precisionTotal digit count to record on the result.
scaleFractional digit count to scale the value to.
Return values
std::nullopttext is not a plain decimal literal, or does not fit the scale.

Definition at line 317 of file SqlDynamicNumeric.hpp.

References precision, scale, and unscaledValue.

Referenced by Lightweight::SqlDataBinder< SqlDynamicNumeric >::FromColumnLiteral().

Member Data Documentation

◆ unscaledValue

std::int64_t Lightweight::SqlDynamicNumeric::unscaledValue = 0

The value, scaled by 10^scale. 12.34 at scale 2 is stored as 1234.

Definition at line 242 of file SqlDynamicNumeric.hpp.

Referenced by FromString(), operator==(), ToDouble(), and ToString().

◆ precision

std::uint8_t Lightweight::SqlDynamicNumeric::precision = 0

Total number of decimal digits the source column holds.

Definition at line 245 of file SqlDynamicNumeric.hpp.

Referenced by FromString().

◆ scale

std::uint8_t Lightweight::SqlDynamicNumeric::scale = 0

Number of digits after the decimal point.

Definition at line 248 of file SqlDynamicNumeric.hpp.

Referenced by FromString(), operator==(), ToDouble(), and ToString().


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