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
impl Decimal
pub fn from_parts(mantissa: BigInt, scale: u32) -> Decimal
pub fn zero() -> Decimal
pub fn from_bigint(value: BigInt) -> Decimal
pub fn mantissa(&self) -> &BigInt
pub fn scale(&self) -> u32
Sourcepub fn parse(source: &str, limits: &Limits) -> Result<Decimal, EngineError>
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.
Sourcepub fn parse_default(source: &str) -> Result<Decimal, EngineError>
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.
pub fn is_zero(&self) -> bool
pub fn is_negative(&self) -> bool
pub fn is_positive(&self) -> bool
pub fn sign(&self) -> Sign
pub fn neg(&self) -> Decimal
pub fn abs(&self) -> Decimal
Sourcepub fn normalized(&self) -> Decimal
pub fn normalized(&self) -> Decimal
Strip trailing fractional zeros. 0.10 becomes 0.1; 100 stays 100.
Sourcepub fn canonical_string(&self) -> String
pub fn canonical_string(&self) -> String
Canonical string used for hashing and cross-representation equality: normalized, plain decimal notation, never exponent notation.
Sourcepub fn to_plain_string(&self) -> String
pub fn to_plain_string(&self) -> String
Display string that preserves the declared scale (0.10 prints as 0.10).
pub fn to_bigint_if_integral(&self) -> Option<BigInt>
pub fn to_rational(&self) -> BigRational
Sourcepub fn to_f64(&self) -> Option<f64>
pub fn to_f64(&self) -> Option<f64>
Approximate binary64 conversion. Returns None if the value overflows.
Sourcepub fn from_f64_display(value: f64) -> Option<Decimal>
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.
Sourcepub fn from_f64_exact(value: f64) -> Option<BigRational>
pub fn from_f64_exact(value: f64) -> Option<BigRational>
Exact conversion of a finite f64 to a rational (used for comparisons).
Sourcepub fn numeric_cmp(&self, other: &Decimal) -> Ordering
pub fn numeric_cmp(&self, other: &Decimal) -> Ordering
Compare two decimals numerically, ignoring scale differences.
pub fn add( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>
pub fn sub( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>
pub fn mul( &self, other: &Decimal, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>
Sourcepub fn div(
&self,
other: &Decimal,
ctx: &NumericContext,
limits: &Limits,
) -> Result<(Decimal, bool), EngineError>
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.
Sourcepub fn round_to(&self, scale: u32, mode: RoundingMode) -> Decimal
pub fn round_to(&self, scale: u32, mode: RoundingMode) -> Decimal
Round to a fixed number of fractional digits.
Sourcepub fn round_significant(&self, digits: u32, mode: RoundingMode) -> Decimal
pub fn round_significant(&self, digits: u32, mode: RoundingMode) -> Decimal
Round to a fixed number of significant digits.
Sourcepub fn round_to_scale(&self, target_scale: i64, mode: RoundingMode) -> Decimal
pub fn round_to_scale(&self, target_scale: i64, mode: RoundingMode) -> Decimal
Round to a (possibly negative) decimal scale.
pub fn floor(&self) -> Decimal
pub fn ceil(&self) -> Decimal
pub fn trunc(&self) -> Decimal
Sourcepub fn sqrt_exact(&self) -> Option<Decimal>
pub fn sqrt_exact(&self) -> Option<Decimal>
Exact square root when the decimal has an exact decimal square root.
Sourcepub fn pow_u32(
&self,
exponent: u32,
ctx: &NumericContext,
limits: &Limits,
) -> Result<Decimal, EngineError>
pub fn pow_u32( &self, exponent: u32, ctx: &NumericContext, limits: &Limits, ) -> Result<Decimal, EngineError>
Integer power. Negative exponents are handled by the caller (division).
Sourcepub fn from_rational(
value: &BigRational,
ctx: &NumericContext,
limits: &Limits,
) -> Result<(Decimal, bool), EngineError>
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.