Skip to main content

Decimal

Struct Decimal 

Source
pub struct Decimal { /* private fields */ }
Expand description

An exact base-10 number: unscaled / 10^scale.

Implementations§

Source§

impl Decimal

Source

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.

Source

pub fn unscaled(&self) -> i128

The unscaled integer digits (value = unscaled × 10^-scale).

Source

pub fn scale(&self) -> u32

Digits after the decimal point, as declared. Preserved exactly: 12.3400 reports 4, not 2.

Source

pub fn precision(&self) -> u32

Total significant digits — the p of DECIMAL(p, s). Zero has precision 1.

Source

pub fn is_zero(&self) -> bool

True when the value is zero at any scale.

Source

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.

Source

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.

Source

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§

Source§

impl Clone for Decimal

Source§

fn clone(&self) -> Decimal

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Decimal

Source§

impl Debug for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Decimal

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The canonical literal, with exactly scale fractional digits — the round-trip form. Decimal::parse(&d.to_string()) == Some(d) for every representable value, trailing zeros included.

Source§

impl Eq for Decimal

Source§

impl Hash for Decimal

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Decimal

Source§

fn eq(&self, other: &Decimal) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
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.

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
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.

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Decimal

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.