pub struct Decimal { /* private fields */ }Expand description
Decimal
Owned fixed-point decimal with an explicit i128 mantissa and base-10 scale.
Arithmetic saturates 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: Decimal) -> Option<Decimal>
pub fn checked_add(self, rhs: Decimal) -> Option<Decimal>
Checked addition; returns None when scale alignment or mantissa
addition overflows the fixed decimal representation.
Sourcepub fn checked_sub(self, rhs: Decimal) -> Option<Decimal>
pub fn checked_sub(self, rhs: Decimal) -> Option<Decimal>
Checked subtraction; returns None when negating the right side,
scale alignment, or mantissa addition overflows.
Sourcepub fn checked_mul(self, rhs: Decimal) -> Option<Decimal>
pub fn checked_mul(self, rhs: Decimal) -> Option<Decimal>
Checked multiplication; returns None when scale or mantissa
multiplication overflows the fixed decimal representation.
Sourcepub fn checked_div(self, rhs: Decimal) -> Option<Decimal>
pub fn checked_div(self, rhs: Decimal) -> Option<Decimal>
Checked division; returns None when the divisor is zero or the
rounded fixed-scale result cannot be represented.
Sourcepub const fn trunc_dp(&self, dp: u32) -> Decimal
pub const fn trunc_dp(&self, dp: u32) -> Decimal
Truncate toward zero to a given number of decimal places.
Sourcepub const fn floor_dp0(&self) -> Decimal
pub const fn floor_dp0(&self) -> Decimal
Return the greatest integral decimal less than or equal to the value.
Sourcepub const fn ceil_dp0(&self) -> Decimal
pub const fn ceil_dp0(&self) -> Decimal
Return the least integral decimal greater than or equal to the value.
Sourcepub fn saturating_add(self, rhs: Decimal) -> Decimal
pub fn saturating_add(self, rhs: Decimal) -> Decimal
Saturating addition.
Sourcepub fn saturating_sub(self, rhs: Decimal) -> Decimal
pub fn saturating_sub(self, rhs: Decimal) -> Decimal
Saturating subtraction.
Sourcepub fn checked_rem(self, rhs: Decimal) -> Option<Decimal>
pub fn checked_rem(self, rhs: Decimal) -> Option<Decimal>
Checked remainder; returns None on division by zero.
Sourcepub const fn checked_abs(&self) -> Option<Decimal>
pub const fn checked_abs(&self) -> Option<Decimal>
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<Decimal>
pub fn checked_powu(&self, exp: u64) -> Option<Decimal>
Checked integer exponentiation using the same exponentiation-by-squaring
shape as powu, but failing instead of saturating on intermediate
multiplication overflow.
Source§impl Decimal
impl Decimal
pub const ZERO: Decimal
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<Decimal>
pub const fn try_new(num: i64, scale: u32) -> Option<Decimal>
Fallible constructor from mantissa and scale.
Sourcepub fn from_num<N>(n: N) -> Option<Decimal>where
N: NumericValue,
pub fn from_num<N>(n: N) -> Option<Decimal>where
N: NumericValue,
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<Decimal>
pub fn from_f32_lossy(n: f32) -> Option<Decimal>
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<Decimal>
pub fn from_f64_lossy(n: f64) -> Option<Decimal>
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<Decimal>
pub const fn try_from_i128_with_scale(num: i128, scale: u32) -> Option<Decimal>
Fallibly build from a raw mantissa and scale.
Sourcepub const fn from_i128_with_scale(num: i128, scale: u32) -> Decimal
pub const fn from_i128_with_scale(num: i128, scale: u32) -> Decimal
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: Decimal)
fn add_assign(&mut self, rhs: Decimal)
+= operation. Read moreSource§impl AddAssign<Decimal> for Usd
impl AddAssign<Decimal> for Usd
Source§fn add_assign(&mut self, other: Decimal)
fn add_assign(&mut self, other: Decimal)
+= operation. Read moreSource§impl AddAssign<Decimal> for E8s
impl AddAssign<Decimal> for E8s
Source§fn add_assign(&mut self, other: Decimal)
fn add_assign(&mut self, other: Decimal)
+= operation. Read moreSource§impl AddAssign<Decimal> for E18s
impl AddAssign<Decimal> for E18s
Source§fn add_assign(&mut self, other: Decimal)
fn add_assign(&mut self, other: Decimal)
+= operation. Read moreSource§impl CandidType for Decimal
impl CandidType for Decimal
impl Copy for Decimal
Source§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Decimal, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Decimal, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for Decimal
impl DivAssign for Decimal
Source§fn div_assign(&mut self, rhs: Decimal)
fn div_assign(&mut self, rhs: Decimal)
/= operation. Read moreSource§impl DivAssign<Decimal> for Usd
impl DivAssign<Decimal> for Usd
Source§fn div_assign(&mut self, other: Decimal)
fn div_assign(&mut self, other: Decimal)
/= operation. Read moreSource§impl DivAssign<Decimal> for E8s
impl DivAssign<Decimal> for E8s
Source§fn div_assign(&mut self, other: Decimal)
fn div_assign(&mut self, other: Decimal)
/= operation. Read moreSource§impl DivAssign<Decimal> for E18s
impl DivAssign<Decimal> for E18s
Source§fn div_assign(&mut self, other: Decimal)
fn div_assign(&mut self, other: Decimal)
/= operation. Read moreimpl Eq for Decimal
Source§impl MulAssign for Decimal
impl MulAssign for Decimal
Source§fn mul_assign(&mut self, rhs: Decimal)
fn mul_assign(&mut self, rhs: Decimal)
*= operation. Read moreSource§impl MulAssign<Decimal> for Usd
impl MulAssign<Decimal> for Usd
Source§fn mul_assign(&mut self, other: Decimal)
fn mul_assign(&mut self, other: Decimal)
*= operation. Read moreSource§impl MulAssign<Decimal> for E8s
impl MulAssign<Decimal> for E8s
Source§fn mul_assign(&mut self, other: Decimal)
fn mul_assign(&mut self, other: Decimal)
*= operation. Read moreSource§impl MulAssign<Decimal> for E18s
impl MulAssign<Decimal> for E18s
Source§fn mul_assign(&mut self, other: Decimal)
fn mul_assign(&mut self, other: Decimal)
*= operation. Read moreSource§impl NormalizeAuto for Decimal
impl NormalizeAuto for Decimal
fn normalize_self(&mut self, _ctx: &mut dyn VisitorContext)
Source§impl NormalizeCustom for Decimal
impl NormalizeCustom for Decimal
fn normalize_custom(&mut self, _ctx: &mut dyn VisitorContext)
Source§impl Normalizer<Decimal> for RoundDecimalPlaces
impl Normalizer<Decimal> for RoundDecimalPlaces
Source§impl NumericValue for Decimal
impl NumericValue for Decimal
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,
Source§impl PartialOrd for Decimal
impl PartialOrd for Decimal
Source§impl PartialOrd<Decimal> for Usd
impl PartialOrd<Decimal> for Usd
Source§impl PartialOrd<Decimal> for E8s
impl PartialOrd<Decimal> for E8s
Source§impl PartialOrd<Decimal> for E18s
impl PartialOrd<Decimal> for E18s
Source§impl PartialOrd<E8s> for Decimal
impl PartialOrd<E8s> for Decimal
Source§impl PartialOrd<E18s> for Decimal
impl PartialOrd<E18s> for Decimal
Source§impl PartialOrd<Usd> for Decimal
impl PartialOrd<Usd> for Decimal
Source§impl Serialize for Decimal
impl Serialize for Decimal
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl SubAssign for Decimal
impl SubAssign for Decimal
Source§fn sub_assign(&mut self, rhs: Decimal)
fn sub_assign(&mut self, rhs: Decimal)
-= operation. Read moreSource§impl SubAssign<Decimal> for Usd
impl SubAssign<Decimal> for Usd
Source§fn sub_assign(&mut self, other: Decimal)
fn sub_assign(&mut self, other: Decimal)
-= operation. Read moreSource§impl SubAssign<Decimal> for E8s
impl SubAssign<Decimal> for E8s
Source§fn sub_assign(&mut self, other: Decimal)
fn sub_assign(&mut self, other: Decimal)
-= operation. Read moreSource§impl SubAssign<Decimal> for E18s
impl SubAssign<Decimal> for E18s
Source§fn sub_assign(&mut self, other: Decimal)
fn sub_assign(&mut self, other: Decimal)
-= operation. Read moreSource§impl ValidateAuto for Decimal
impl ValidateAuto for Decimal
fn validate_self(&self, _ctx: &mut dyn VisitorContext)
Source§impl ValidateCustom for Decimal
impl ValidateCustom for Decimal
fn validate_custom(&self, _ctx: &mut dyn VisitorContext)
Source§impl Validator<Decimal> for MaxDecimalPlaces
impl Validator<Decimal> for MaxDecimalPlaces
fn validate(&self, n: &Decimal, ctx: &mut dyn VisitorContext)
Auto Trait Implementations§
impl Freeze for Decimal
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnsafeUnpin for Decimal
impl UnwindSafe for Decimal
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> CustomError for T
impl<T> CustomError for T
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.