pub struct Decimal(/* private fields */);Expand description
A 128-bit decimal number with deterministic arithmetic.
This type wraps rust_decimal::Decimal and provides checked arithmetic
operations that explicitly handle overflow, underflow, and division by zero.
All operations are deterministic across platforms.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub const NEGATIVE_ONE: Self
pub const NEGATIVE_ONE: Self
Negative one.
Sourcepub const ONE_HUNDRED: Self
pub const ONE_HUNDRED: Self
One hundred.
Sourcepub const ONE_THOUSAND: Self
pub const ONE_THOUSAND: Self
One thousand.
Sourcepub fn new(mantissa: i64, scale: u32) -> Self
pub fn new(mantissa: i64, scale: u32) -> Self
Creates a new decimal from integer mantissa and scale.
The value is mantissa * 10^(-scale).
§Panics
Panics if scale exceeds 28.
Sourcepub const fn from_parts(
lo: u32,
mid: u32,
hi: u32,
negative: bool,
scale: u32,
) -> Self
pub const fn from_parts( lo: u32, mid: u32, hi: u32, negative: bool, scale: u32, ) -> Self
Creates a decimal from raw parts.
The 96-bit mantissa is stored as three 32-bit words in little-endian order.
The sign is true for negative values.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Returns true if the value is negative.
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Returns true if the value is positive.
Sourcepub fn checked_add(self, other: Self) -> Option<Self>
pub fn checked_add(self, other: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcepub fn checked_sub(self, other: Self) -> Option<Self>
pub fn checked_sub(self, other: Self) -> Option<Self>
Checked subtraction. Returns None on overflow.
Sourcepub fn checked_mul(self, other: Self) -> Option<Self>
pub fn checked_mul(self, other: Self) -> Option<Self>
Checked multiplication. Returns None on overflow.
Sourcepub fn checked_div(self, other: Self) -> Option<Self>
pub fn checked_div(self, other: Self) -> Option<Self>
Checked division. Returns None on division by zero or overflow.
Sourcepub fn checked_rem(self, other: Self) -> Option<Self>
pub fn checked_rem(self, other: Self) -> Option<Self>
Checked remainder. Returns None on division by zero.
Sourcepub fn saturating_add(self, other: Self) -> Self
pub fn saturating_add(self, other: Self) -> Self
Saturating addition. Returns MAX or MIN on overflow.
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Saturating subtraction. Returns MAX or MIN on overflow.
Sourcepub fn saturating_mul(self, other: Self) -> Self
pub fn saturating_mul(self, other: Self) -> Self
Saturating multiplication. Returns MAX or MIN on overflow.
Sourcepub fn try_add(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_add(self, other: Self) -> Result<Self, ArithmeticError>
Addition with explicit error on overflow.
Sourcepub fn try_sub(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_sub(self, other: Self) -> Result<Self, ArithmeticError>
Subtraction with explicit error on overflow.
Sourcepub fn try_mul(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_mul(self, other: Self) -> Result<Self, ArithmeticError>
Multiplication with explicit error on overflow.
Sourcepub fn try_div(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_div(self, other: Self) -> Result<Self, ArithmeticError>
Division with explicit error handling.
Sourcepub fn round(self, dp: u32, mode: RoundingMode) -> Self
pub fn round(self, dp: u32, mode: RoundingMode) -> Self
Rounds to the specified number of decimal places using the given mode.
Sourcepub fn round_dp(self, dp: u32) -> Self
pub fn round_dp(self, dp: u32) -> Self
Rounds to the specified number of decimal places using banker’s rounding.
Sourcepub fn rescale(&mut self, scale: u32) -> Result<(), ArithmeticError>
pub fn rescale(&mut self, scale: u32) -> Result<(), ArithmeticError>
Rescales to the specified number of decimal places.
Returns an error if the scale would exceed MAX_SCALE.
Sourcepub fn into_inner(self) -> RustDecimal
pub fn into_inner(self) -> RustDecimal
Returns the internal representation for interop.
Sourcepub fn from_inner(inner: RustDecimal) -> Self
pub fn from_inner(inner: RustDecimal) -> Self
Creates from the internal representation.