pub struct Decimal { /* private fields */ }Expand description
Decimal
Owned fixed-point decimal with an explicit i128 mantissa and base-10 scale. Wire and Serde encode and decode decimal text in every format.
Arithmetic saturates at the scale-zero magnitude bounds on overflow,
division by zero resolves to ZERO, and
normalization keeps equivalent values on one canonical representation.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition rounds half away from zero to the greatest fitting
scale, starting at the greater operand scale. Returns None only when
the rounded magnitude cannot fit even at scale zero.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction uses the same fitting/rounding policy as addition.
Returns None when the rounded magnitude cannot fit at scale zero.
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication normalizes operand padding and rounds half away
from zero to the greatest representable scale, at most 28. Returns None
when the rounded magnitude cannot fit even at scale zero.
Sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Checked division rounds half away from zero to the greatest fitting
scale, at most 18. Returns None when the divisor is zero or the rounded
magnitude cannot fit even at scale zero.
Sourcepub const fn trunc_dp(&self, dp: u32) -> Self
pub const fn trunc_dp(&self, dp: u32) -> Self
Truncate toward zero to a given number of decimal places.
Sourcepub const fn floor_dp0(&self) -> Self
pub const fn floor_dp0(&self) -> Self
Return the greatest integral decimal less than or equal to the value.
Sourcepub const fn ceil_dp0(&self) -> Self
pub const fn ceil_dp0(&self) -> Self
Return the least integral decimal greater than or equal to the value.
Sourcepub fn saturating_add(self, rhs: Self) -> Self
pub fn saturating_add(self, rhs: Self) -> Self
Saturating addition.
Sourcepub fn saturating_sub(self, rhs: Self) -> Self
pub fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction.
Sourcepub fn checked_rem(self, rhs: Self) -> Option<Self>
pub fn checked_rem(self, rhs: Self) -> Option<Self>
Exact remainder with the dividend’s sign, at the greater operand scale.
Returns None on division by zero; scale alignment cannot overflow.
Sourcepub const fn checked_abs(&self) -> Option<Self>
pub const fn checked_abs(&self) -> Option<Self>
Checked absolute value; returns None for the one i128::MIN
mantissa case that cannot be represented as positive i128.
Sourcepub fn checked_powu(&self, exp: u64) -> Option<Self>
pub fn checked_powu(&self, exp: u64) -> Option<Self>
Checked integer exponentiation using the same exponentiation-by-squaring
shape as powu, but failing instead of saturating on intermediate
magnitude overflow. Each multiplication uses the same rounded
fixed-representation contract as checked_mul.
Source§impl Decimal
impl Decimal
pub const ZERO: Self
Sourcepub const fn max_supported_scale() -> u32
pub const fn max_supported_scale() -> u32
Returns the maximum supported decimal scale.
Sourcepub const fn try_new(num: i64, scale: u32) -> Option<Self>
pub const fn try_new(num: i64, scale: u32) -> Option<Self>
Fallible constructor from mantissa and scale.
Sourcepub fn from_num<N: NumericValue>(n: N) -> Option<Self>
pub fn from_num<N: NumericValue>(n: N) -> Option<Self>
Fallible conversion from common numeric types.
This path is lossy for float inputs and may lose precision for large values.
Prefer exact integer constructors (from_i64, from_u64) or explicit
float constructors (from_f32_lossy, from_f64_lossy) when possible.
Sourcepub fn from_f32_lossy(n: f32) -> Option<Self>
pub fn from_f32_lossy(n: f32) -> Option<Self>
Explicit lossy conversion from f32.
Uses decimal text round-tripping from the binary float representation. This is intentionally lossy and should be used only when float input is required.
Sourcepub fn from_f64_lossy(n: f64) -> Option<Self>
pub fn from_f64_lossy(n: f64) -> Option<Self>
Explicit lossy conversion from f64.
Uses decimal text round-tripping from the binary float representation. This is intentionally lossy and should be used only when float input is required.
Sourcepub const fn parts(&self) -> DecimalParts
pub const fn parts(&self) -> DecimalParts
PARTS
Decompose into mantissa and scale.
Sourcepub const fn is_integer(&self) -> bool
pub const fn is_integer(&self) -> bool
Returns true if the decimal has no fractional component.
Sourcepub fn scale_to_integer(&self, target_scale: u32) -> Option<i128>
pub fn scale_to_integer(&self, target_scale: u32) -> Option<i128>
Scale by 10^target_scale and require an integer result.
Returns None if:
- fractional precision would be lost
- integer overflow occurs
Sourcepub fn to_u128(&self) -> Option<u128>
pub fn to_u128(&self) -> Option<u128>
Convert to u128 when the decimal is integral and in range.
Sourcepub const fn try_from_i128_with_scale(num: i128, scale: u32) -> Option<Self>
pub const fn try_from_i128_with_scale(num: i128, scale: u32) -> Option<Self>
Fallibly build from a raw mantissa and scale.
Sourcepub const fn from_i128_with_scale(num: i128, scale: u32) -> Self
pub const fn from_i128_with_scale(num: i128, scale: u32) -> Self
Build from a raw mantissa and scale.
§Panics
Panics when the mantissa and scale cannot be represented without violating the decimal scale invariant.
Sourcepub const fn is_sign_negative(&self) -> bool
pub const fn is_sign_negative(&self) -> bool
Returns true if the value is negative.
Trait Implementations§
Source§impl AddAssign for Decimal
impl AddAssign for Decimal
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreimpl Copy for Decimal
Source§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for Decimal
impl DivAssign for Decimal
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreimpl Eq for Decimal
Source§impl MulAssign for Decimal
impl MulAssign for Decimal
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl NumericValue for Decimal
impl NumericValue for Decimal
Source§fn try_to_decimal(&self) -> Option<Self>
fn try_to_decimal(&self) -> Option<Self>
Decimal for generic numeric handling.Source§fn try_from_decimal(value: Decimal) -> Option<Self>
fn try_from_decimal(value: Decimal) -> Option<Self>
Decimal after generic numeric handling.Source§impl Ord for Decimal
impl Ord for Decimal
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Decimal
impl PartialOrd for Decimal
Source§impl RemAssign for Decimal
impl RemAssign for Decimal
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read more