pub struct Decimal { /* private fields */ }Expand description
An exact base-10 number: unscaled / 10^scale.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub fn from_parts(unscaled: i128, scale: u32) -> Option<Self>
pub fn from_parts(unscaled: i128, scale: u32) -> Option<Self>
Construct from raw parts. None if scale exceeds the maximum
precision, which would make the value unrepresentable.
Sourcepub fn scale(&self) -> u32
pub fn scale(&self) -> u32
Digits after the decimal point, as declared. Preserved exactly:
12.3400 reports 4, not 2.
Sourcepub fn precision(&self) -> u32
pub fn precision(&self) -> u32
Total significant digits — the p of DECIMAL(p, s). Zero has
precision 1.
Sourcepub fn parse(s: &str) -> Option<Self>
pub fn parse(s: &str) -> Option<Self>
Parse a decimal literal: optional sign, digits, optional
fraction, optional e±nn exponent. The exponent is folded into
the scale, so 1.5e3 parses as 1500 and 15e-3 as 0.015 —
both exact.
Returns None for anything that is not a decimal literal, or
that needs more than 38 significant digits. NEVER falls back to
float parsing: silently accepting a value we cannot represent
exactly is the bug this type exists to prevent.
Sourcepub fn rescale(&self, target: u32) -> Option<Self>
pub fn rescale(&self, target: u32) -> Option<Self>
Rescale to target digits after the point. None when that
would drop non-zero digits (an inexact narrowing) or overflow.
Sourcepub fn cmp_value(&self, other: &Self) -> Ordering
pub fn cmp_value(&self, other: &Self) -> Ordering
Exact ordering, independent of scale: 1.50 equals 1.5 in
value even though they are distinct column values.
Compares by aligning scales in i128; if alignment would
overflow it falls back to comparing sign, integer-digit count,
and then digits pairwise — which needs no arithmetic at all and
so is exact at any magnitude.
Trait Implementations§
impl Copy for Decimal
Source§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
impl Eq for Decimal
Source§impl PartialOrd for Decimal
Value ordering, NOT the derived field order — 1.50 and 1.5 are
Ordering::Equal here while remaining distinct under Eq.
impl PartialOrd for Decimal
Value ordering, NOT the derived field order — 1.50 and 1.5 are
Ordering::Equal here while remaining distinct under Eq.
Source§impl Serialize for Decimal
Serialized as its canonical STRING, never a JSON number: a JSON
number goes through f64 in most parsers (including JavaScript),
which is exactly the corruption this type exists to prevent.
impl Serialize for Decimal
Serialized as its canonical STRING, never a JSON number: a JSON
number goes through f64 in most parsers (including JavaScript),
which is exactly the corruption this type exists to prevent.