Skip to main content

Decimal

Struct Decimal 

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

An exact decimal: mantissa * 10^-scale.

The scale is preserved as given so that 0.10 remains distinguishable from 0.1 for presentation and fingerprinting, while comparisons are numeric.

Implementations§

Source§

impl Decimal

Source

pub fn from_parts(mantissa: BigInt, scale: u32) -> Decimal

Source

pub fn zero() -> Decimal

Source

pub fn from_bigint(value: BigInt) -> Decimal

Source

pub fn mantissa(&self) -> &BigInt

Source

pub fn scale(&self) -> u32

Source

pub fn parse(source: &str, limits: &Limits) -> Result<Decimal, EngineError>

Parse a locale-independent decimal literal.

Grammar: [+-]? ( digits ('.' digits?)? | '.' digits ) ([eE] [+-]? digits)? No whitespace, no thousands separators, no locale decimal comma.

Source

pub fn parse_default(source: &str) -> Result<Decimal, EngineError>

Parse using conservative default limits. Intended for tests and constants; public entry points must pass explicit limits.

Source

pub fn is_zero(&self) -> bool

Source

pub fn is_negative(&self) -> bool

Source

pub fn is_positive(&self) -> bool

Source

pub fn sign(&self) -> Sign

Source

pub fn neg(&self) -> Decimal

Source

pub fn abs(&self) -> Decimal

Source

pub fn normalized(&self) -> Decimal

Strip trailing fractional zeros. 0.10 becomes 0.1; 100 stays 100.

Source

pub fn canonical_string(&self) -> String

Canonical string used for hashing and cross-representation equality: normalized, plain decimal notation, never exponent notation.

Source

pub fn to_plain_string(&self) -> String

Display string that preserves the declared scale (0.10 prints as 0.10).

Source

pub fn to_bigint_if_integral(&self) -> Option<BigInt>

Source

pub fn to_rational(&self) -> BigRational

Source

pub fn to_f64(&self) -> Option<f64>

Approximate binary64 conversion. Returns None if the value overflows.

Source

pub fn from_f64_display(value: f64) -> Option<Decimal>

Decimal representation of the shortest round-tripping string of value. This is a display conversion, not an exact binary-to-decimal conversion.

Source

pub fn from_f64_exact(value: f64) -> Option<BigRational>

Exact conversion of a finite f64 to a rational (used for comparisons).

Source

pub fn numeric_cmp(&self, other: &Decimal) -> Ordering

Compare two decimals numerically, ignoring scale differences.

Source

pub fn add( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>

Source

pub fn sub( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>

Source

pub fn mul( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>

Source

pub fn div( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<(Decimal, bool), EngineError>

Divide under the declared context. Returns (value, inexact).

When the quotient terminates it is returned exactly with trailing zeros stripped. Otherwise the quotient is rounded to ctx.precision significant digits and inexact is true.

Source

pub fn round_to(&self, scale: u32, mode: RoundingMode) -> Decimal

Round to a fixed number of fractional digits.

Source

pub fn round_significant(&self, digits: u32, mode: RoundingMode) -> Decimal

Round to a fixed number of significant digits.

Source

pub fn round_to_scale(&self, target_scale: i64, mode: RoundingMode) -> Decimal

Round to a (possibly negative) decimal scale.

Source

pub fn floor(&self) -> Decimal

Source

pub fn ceil(&self) -> Decimal

Source

pub fn trunc(&self) -> Decimal

Source

pub fn sqrt_exact(&self) -> Option<Decimal>

Exact square root when the decimal has an exact decimal square root.

Source

pub fn pow_u32( &self, exponent: u32, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>

Integer power. Negative exponents are handled by the caller (division).

Source

pub fn from_rational( value: &BigRational, ctx: &NumericContext, limits: &Limits, ) -> Result<(Decimal, bool), EngineError>

Exact conversion from a rational, or a rounded conversion when allowed.

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 Debug for Decimal

Source§

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

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

impl Display for Decimal

Source§

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

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

impl Eq for Decimal

Source§

impl Ord for Decimal

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Decimal

Source§

fn eq(&self, other: &Self) -> 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

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

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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.