pub struct Decimal<B: Backing, const DECIMALS: u8> { /* private fields */ }Expand description
Fixed-point decimal with compile-time backing type and precision.
B is the backing integer type (i32, i64, i128).
DECIMALS is the number of fractional digits. Any combination
where 10^DECIMALS fits in B is valid — Decimal<i64, 2> for
USD or Decimal<i64, 8> for BTC without any macro invocation.
The scale factor 10^DECIMALS is validated at compile time.
Invalid combinations (e.g., Decimal<i32, 10>) fail to compile
when any associated constant or method is used.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
const PRICE: D64 = D64::new(100, 50_000_000); // 100.50
const FEE: D64 = D64::from_raw(500_000); // 0.005
const TOTAL: D64 = match PRICE.checked_add(FEE) {
Some(v) => v,
None => panic!("overflow"),
};Implementations§
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
§Overflow behavior
The absolute value of Self::MIN cannot be represented as
a Self, and attempting to calculate it will cause an
overflow. This means that code in debug mode will trigger
a panic on this case and optimized code will return
Self::MIN without a panic.
Matches the semantics of <backing>::abs. Use
checked_abs,
saturating_abs, or
wrapping_abs for explicit overflow
policies.
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None on overflow.
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Returns None if self == MIN.
Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value. Returns None if self == MIN.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Clamps to MIN/MAX on overflow.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction.
Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation.
Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction.
Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping negation.
Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value.
Sourcepub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
Addition returning Result.
Sourcepub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
Subtraction returning Result.
Sourcepub const fn try_neg(self) -> Result<Self, OverflowError>
pub const fn try_neg(self) -> Result<Self, OverflowError>
Negation returning Result.
Sourcepub const fn try_abs(self) -> Result<Self, OverflowError>
pub const fn try_abs(self) -> Result<Self, OverflowError>
Absolute value returning Result.
Sourcepub const fn mul_pow2(self, n: u32) -> Self
pub const fn mul_pow2(self, n: u32) -> Self
Multiply by 2^n (left shift on the backing value).
The 10^D scale factor cancels because the multiplier is
dimensionless — multiplying the represented value by 2^n is
exactly a left shift on the backing.
§Overflow behavior
Matches <backing>::mul semantics: debug builds panic on
overflow, release builds wrap (wrapping_shl, which masks
n to n mod <backing>::BITS). Use
checked_mul_pow2,
saturating_mul_pow2, or
wrapping_mul_pow2 for explicit
overflow policies.
In particular, mul_pow2(v, BITS) in release returns v
unchanged — the mask makes this a no-op, not a zeroing.
§Codegen
Lowers to a single backing-width shift in release builds
(both constant and variable n). For i32 / i64
backings this is one instruction (~1 cycle); for i128
it expands to a branchless wide-shift sequence (shld +
shl on x86-64, ~4-5 cycles).
Sourcepub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
pub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
Checked multiplication by 2^n. Returns None on overflow.
Uses leading-zero counting to detect overflow without
performing the shift first. For positive v, requires
n < v.leading_zeros(); for negative v, requires
n < (!v).leading_zeros().
Sourcepub const fn saturating_mul_pow2(self, n: u32) -> Self
pub const fn saturating_mul_pow2(self, n: u32) -> Self
Sourcepub const fn wrapping_mul_pow2(self, n: u32) -> Self
pub const fn wrapping_mul_pow2(self, n: u32) -> Self
Wrapping multiplication by 2^n.
Silently wraps on overflow. Note that wrapping_shl masks
n to n mod <backing>::BITS, so e.g. shifting by BITS
is a no-op rather than zeroing the value.
Sourcepub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
pub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
Multiplication by 2^n returning Result.
Sourcepub const fn div_pow2(self, n: u32) -> Self
pub const fn div_pow2(self, n: u32) -> Self
Divide by 2^n (truncate toward zero).
Semantically identical to / 2^n: truncates toward zero,
matching halve, div10,
div100, and the rest of the division
surface. Invariant: div_pow2(1) == halve().
§Codegen
Constant n folds to a branchless shift + sign-correction
sequence (~2 cycles on modern x86-64). Variable n
compiles to a hardware signed division (~8-12 cycles on
Ice Lake+ / Zen 3+) — use a constant when the shift
amount is known.
§Panics
Debug builds panic if n >= <backing>::BITS. Release
builds return ZERO, which is the
mathematically correct result under truncate-toward-zero:
any value divided by 2^n larger than its magnitude is 0.
Sourcepub const fn checked_abs_diff(self, other: Self) -> Option<Self>
pub const fn checked_abs_diff(self, other: Self) -> Option<Self>
Overflow-safe absolute difference: |self - other|.
Returns None when the result would exceed
MAX — this happens when the operands have
opposite signs near the rails, since |MIN - MAX| exceeds
MAX on every signed type.
Named checked_abs_diff to match the crate’s checked_*
convention for Option-returning operations. There is no
bare abs_diff — every call site must acknowledge the
overflow case. Stdlib’s <backing>::abs_diff returns an
unsigned type to avoid overflow; since Decimal has no
unsigned variant, this returns Option<Self> instead.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
§Overflow behavior
The absolute value of Self::MIN cannot be represented as
a Self, and attempting to calculate it will cause an
overflow. This means that code in debug mode will trigger
a panic on this case and optimized code will return
Self::MIN without a panic.
Matches the semantics of <backing>::abs. Use
checked_abs,
saturating_abs, or
wrapping_abs for explicit overflow
policies.
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None on overflow.
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Returns None if self == MIN.
Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value. Returns None if self == MIN.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Clamps to MIN/MAX on overflow.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction.
Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation.
Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction.
Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping negation.
Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value.
Sourcepub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
Addition returning Result.
Sourcepub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
Subtraction returning Result.
Sourcepub const fn try_neg(self) -> Result<Self, OverflowError>
pub const fn try_neg(self) -> Result<Self, OverflowError>
Negation returning Result.
Sourcepub const fn try_abs(self) -> Result<Self, OverflowError>
pub const fn try_abs(self) -> Result<Self, OverflowError>
Absolute value returning Result.
Sourcepub const fn mul_pow2(self, n: u32) -> Self
pub const fn mul_pow2(self, n: u32) -> Self
Multiply by 2^n (left shift on the backing value).
The 10^D scale factor cancels because the multiplier is
dimensionless — multiplying the represented value by 2^n is
exactly a left shift on the backing.
§Overflow behavior
Matches <backing>::mul semantics: debug builds panic on
overflow, release builds wrap (wrapping_shl, which masks
n to n mod <backing>::BITS). Use
checked_mul_pow2,
saturating_mul_pow2, or
wrapping_mul_pow2 for explicit
overflow policies.
In particular, mul_pow2(v, BITS) in release returns v
unchanged — the mask makes this a no-op, not a zeroing.
§Codegen
Lowers to a single backing-width shift in release builds
(both constant and variable n). For i32 / i64
backings this is one instruction (~1 cycle); for i128
it expands to a branchless wide-shift sequence (shld +
shl on x86-64, ~4-5 cycles).
Sourcepub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
pub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
Checked multiplication by 2^n. Returns None on overflow.
Uses leading-zero counting to detect overflow without
performing the shift first. For positive v, requires
n < v.leading_zeros(); for negative v, requires
n < (!v).leading_zeros().
Sourcepub const fn saturating_mul_pow2(self, n: u32) -> Self
pub const fn saturating_mul_pow2(self, n: u32) -> Self
Sourcepub const fn wrapping_mul_pow2(self, n: u32) -> Self
pub const fn wrapping_mul_pow2(self, n: u32) -> Self
Wrapping multiplication by 2^n.
Silently wraps on overflow. Note that wrapping_shl masks
n to n mod <backing>::BITS, so e.g. shifting by BITS
is a no-op rather than zeroing the value.
Sourcepub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
pub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
Multiplication by 2^n returning Result.
Sourcepub const fn div_pow2(self, n: u32) -> Self
pub const fn div_pow2(self, n: u32) -> Self
Divide by 2^n (truncate toward zero).
Semantically identical to / 2^n: truncates toward zero,
matching halve, div10,
div100, and the rest of the division
surface. Invariant: div_pow2(1) == halve().
§Codegen
Constant n folds to a branchless shift + sign-correction
sequence (~2 cycles on modern x86-64). Variable n
compiles to a hardware signed division (~8-12 cycles on
Ice Lake+ / Zen 3+) — use a constant when the shift
amount is known.
§Panics
Debug builds panic if n >= <backing>::BITS. Release
builds return ZERO, which is the
mathematically correct result under truncate-toward-zero:
any value divided by 2^n larger than its magnitude is 0.
Sourcepub const fn checked_abs_diff(self, other: Self) -> Option<Self>
pub const fn checked_abs_diff(self, other: Self) -> Option<Self>
Overflow-safe absolute difference: |self - other|.
Returns None when the result would exceed
MAX — this happens when the operands have
opposite signs near the rails, since |MIN - MAX| exceeds
MAX on every signed type.
Named checked_abs_diff to match the crate’s checked_*
convention for Option-returning operations. There is no
bare abs_diff — every call site must acknowledge the
overflow case. Stdlib’s <backing>::abs_diff returns an
unsigned type to avoid overflow; since Decimal has no
unsigned variant, this returns Option<Self> instead.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Computes the absolute value of self.
§Overflow behavior
The absolute value of Self::MIN cannot be represented as
a Self, and attempting to calculate it will cause an
overflow. This means that code in debug mode will trigger
a panic on this case and optimized code will return
Self::MIN without a panic.
Matches the semantics of <backing>::abs. Use
checked_abs,
saturating_abs, or
wrapping_abs for explicit overflow
policies.
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction. Returns None on overflow.
Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Returns None if self == MIN.
Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value. Returns None if self == MIN.
Sourcepub const fn saturating_add(self, rhs: Self) -> Self
pub const fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Clamps to MIN/MAX on overflow.
Sourcepub const fn saturating_sub(self, rhs: Self) -> Self
pub const fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction.
Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation.
Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value.
Sourcepub const fn wrapping_add(self, rhs: Self) -> Self
pub const fn wrapping_add(self, rhs: Self) -> Self
Wrapping addition.
Sourcepub const fn wrapping_sub(self, rhs: Self) -> Self
pub const fn wrapping_sub(self, rhs: Self) -> Self
Wrapping subtraction.
Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping negation.
Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value.
Sourcepub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_add(self, rhs: Self) -> Result<Self, OverflowError>
Addition returning Result.
Sourcepub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_sub(self, rhs: Self) -> Result<Self, OverflowError>
Subtraction returning Result.
Sourcepub const fn try_neg(self) -> Result<Self, OverflowError>
pub const fn try_neg(self) -> Result<Self, OverflowError>
Negation returning Result.
Sourcepub const fn try_abs(self) -> Result<Self, OverflowError>
pub const fn try_abs(self) -> Result<Self, OverflowError>
Absolute value returning Result.
Sourcepub const fn mul_pow2(self, n: u32) -> Self
pub const fn mul_pow2(self, n: u32) -> Self
Multiply by 2^n (left shift on the backing value).
The 10^D scale factor cancels because the multiplier is
dimensionless — multiplying the represented value by 2^n is
exactly a left shift on the backing.
§Overflow behavior
Matches <backing>::mul semantics: debug builds panic on
overflow, release builds wrap (wrapping_shl, which masks
n to n mod <backing>::BITS). Use
checked_mul_pow2,
saturating_mul_pow2, or
wrapping_mul_pow2 for explicit
overflow policies.
In particular, mul_pow2(v, BITS) in release returns v
unchanged — the mask makes this a no-op, not a zeroing.
§Codegen
Lowers to a single backing-width shift in release builds
(both constant and variable n). For i32 / i64
backings this is one instruction (~1 cycle); for i128
it expands to a branchless wide-shift sequence (shld +
shl on x86-64, ~4-5 cycles).
Sourcepub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
pub const fn checked_mul_pow2(self, n: u32) -> Option<Self>
Checked multiplication by 2^n. Returns None on overflow.
Uses leading-zero counting to detect overflow without
performing the shift first. For positive v, requires
n < v.leading_zeros(); for negative v, requires
n < (!v).leading_zeros().
Sourcepub const fn saturating_mul_pow2(self, n: u32) -> Self
pub const fn saturating_mul_pow2(self, n: u32) -> Self
Sourcepub const fn wrapping_mul_pow2(self, n: u32) -> Self
pub const fn wrapping_mul_pow2(self, n: u32) -> Self
Wrapping multiplication by 2^n.
Silently wraps on overflow. Note that wrapping_shl masks
n to n mod <backing>::BITS, so e.g. shifting by BITS
is a no-op rather than zeroing the value.
Sourcepub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
pub const fn try_mul_pow2(self, n: u32) -> Result<Self, OverflowError>
Multiplication by 2^n returning Result.
Sourcepub const fn div_pow2(self, n: u32) -> Self
pub const fn div_pow2(self, n: u32) -> Self
Divide by 2^n (truncate toward zero).
Semantically identical to / 2^n: truncates toward zero,
matching halve, div10,
div100, and the rest of the division
surface. Invariant: div_pow2(1) == halve().
§Codegen
Constant n folds to a branchless shift + sign-correction
sequence (~2 cycles on modern x86-64). Variable n
compiles to a hardware signed division (~8-12 cycles on
Ice Lake+ / Zen 3+) — use a constant when the shift
amount is known.
§Panics
Debug builds panic if n >= <backing>::BITS. Release
builds return ZERO, which is the
mathematically correct result under truncate-toward-zero:
any value divided by 2^n larger than its magnitude is 0.
Sourcepub const fn checked_abs_diff(self, other: Self) -> Option<Self>
pub const fn checked_abs_diff(self, other: Self) -> Option<Self>
Overflow-safe absolute difference: |self - other|.
Returns None when the result would exceed
MAX — this happens when the operands have
opposite signs near the rails, since |MIN - MAX| exceeds
MAX on every signed type.
Named checked_abs_diff to match the crate’s checked_*
convention for Option-returning operations. There is no
bare abs_diff — every call site must acknowledge the
overflow case. Stdlib’s <backing>::abs_diff returns an
unsigned type to avoid overflow; since Decimal has no
unsigned variant, this returns Option<Self> instead.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication. Widens to i64, divides by SCALE.
Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Checked division. Returns None if rhs is zero or result overflows.
Sourcepub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn saturating_mul(self, rhs: Self) -> Self
Saturating multiplication.
Sourcepub const fn wrapping_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication.
Sourcepub const fn saturating_div(self, rhs: Self) -> Self
pub const fn saturating_div(self, rhs: Self) -> Self
Saturating division.
Sourcepub const fn wrapping_div(self, rhs: Self) -> Self
pub const fn wrapping_div(self, rhs: Self) -> Self
Wrapping division.
Sourcepub const fn mul_int(self, rhs: i32) -> Option<Self>
pub const fn mul_int(self, rhs: i32) -> Option<Self>
Multiply by a plain integer (no rescaling).
Sourcepub const fn mul_add(self, mul: Self, add: Self) -> Option<Self>
pub const fn mul_add(self, mul: Self, add: Self) -> Option<Self>
Fused multiply-add: (self * mul) + add with single rescaling.
Sourcepub const fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
Multiplication returning Result.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn checked_mul(self, rhs: Self) -> Option<Self>
pub const fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication. Widens to i128, divides by SCALE.
Sourcepub const fn checked_div(self, rhs: Self) -> Option<Self>
pub const fn checked_div(self, rhs: Self) -> Option<Self>
Checked division. Returns None if rhs is zero or result overflows.
Division by a runtime value cannot use the chunked path — the divisor isn’t a compile-time constant. Uses native i128 division.
Sourcepub const fn saturating_mul(self, rhs: Self) -> Self
pub const fn saturating_mul(self, rhs: Self) -> Self
Saturating multiplication.
Sourcepub const fn wrapping_mul(self, rhs: Self) -> Self
pub const fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication.
Sourcepub const fn saturating_div(self, rhs: Self) -> Self
pub const fn saturating_div(self, rhs: Self) -> Self
Saturating division.
Sourcepub const fn wrapping_div(self, rhs: Self) -> Self
pub const fn wrapping_div(self, rhs: Self) -> Self
Wrapping division.
Sourcepub const fn mul_int(self, rhs: i64) -> Option<Self>
pub const fn mul_int(self, rhs: i64) -> Option<Self>
Multiply by a plain integer (no rescaling).
Sourcepub const fn mul_add(self, mul: Self, add: Self) -> Option<Self>
pub const fn mul_add(self, mul: Self, add: Self) -> Option<Self>
Fused multiply-add: (self * mul) + add with single rescaling.
Sourcepub const fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
pub const fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
Multiplication returning Result.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Checked multiplication using 192-bit wide arithmetic.
Sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Checked division using 192-bit wide arithmetic.
Sourcepub fn saturating_mul(self, rhs: Self) -> Self
pub fn saturating_mul(self, rhs: Self) -> Self
Saturating multiplication.
Sourcepub fn wrapping_mul(self, rhs: Self) -> Self
pub fn wrapping_mul(self, rhs: Self) -> Self
Wrapping multiplication.
Sourcepub fn saturating_div(self, rhs: Self) -> Self
pub fn saturating_div(self, rhs: Self) -> Self
Saturating division.
Sourcepub fn wrapping_div(self, rhs: Self) -> Self
pub fn wrapping_div(self, rhs: Self) -> Self
Wrapping division.
Sourcepub const fn mul_int(self, rhs: i128) -> Option<Self>
pub const fn mul_int(self, rhs: i128) -> Option<Self>
Multiply by a plain integer (no rescaling).
Sourcepub fn mul_add(self, mul: Self, add: Self) -> Option<Self>
pub fn mul_add(self, mul: Self, add: Self) -> Option<Self>
Fused multiply-add: (self * mul) + add with single rescaling.
Sourcepub fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
pub fn try_mul(self, rhs: Self) -> Result<Self, OverflowError>
Multiplication returning Result.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn to_le_bytes(self) -> [u8; 4]
pub const fn to_le_bytes(self) -> [u8; 4]
Returns the underlying value as little-endian bytes.
Sourcepub const fn to_be_bytes(self) -> [u8; 4]
pub const fn to_be_bytes(self) -> [u8; 4]
Returns the underlying value as big-endian bytes.
Sourcepub const fn to_ne_bytes(self) -> [u8; 4]
pub const fn to_ne_bytes(self) -> [u8; 4]
Returns the underlying value as native-endian bytes.
Sourcepub const fn from_le_bytes(bytes: [u8; 4]) -> Self
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self
Reconstructs a Decimal from its little-endian byte representation.
Sourcepub const fn from_be_bytes(bytes: [u8; 4]) -> Self
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self
Reconstructs a Decimal from its big-endian byte representation.
Sourcepub const fn from_ne_bytes(bytes: [u8; 4]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self
Reconstructs a Decimal from its native-endian byte representation.
Sourcepub fn write_le_bytes(&self, buf: &mut [u8])
pub fn write_le_bytes(&self, buf: &mut [u8])
Writes little-endian bytes into buf. Panics if buf.len() < 4.
Sourcepub fn write_be_bytes(&self, buf: &mut [u8])
pub fn write_be_bytes(&self, buf: &mut [u8])
Writes big-endian bytes into buf. Panics if buf.len() < 4.
Sourcepub fn read_le_bytes(buf: &[u8]) -> Self
pub fn read_le_bytes(buf: &[u8]) -> Self
Reads little-endian bytes from buf. Panics if buf.len() < 4.
Sourcepub fn read_be_bytes(buf: &[u8]) -> Self
pub fn read_be_bytes(buf: &[u8]) -> Self
Reads big-endian bytes from buf. Panics if buf.len() < 4.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn to_le_bytes(self) -> [u8; 8]
pub const fn to_le_bytes(self) -> [u8; 8]
Returns the underlying value as little-endian bytes.
Sourcepub const fn to_be_bytes(self) -> [u8; 8]
pub const fn to_be_bytes(self) -> [u8; 8]
Returns the underlying value as big-endian bytes.
Sourcepub const fn to_ne_bytes(self) -> [u8; 8]
pub const fn to_ne_bytes(self) -> [u8; 8]
Returns the underlying value as native-endian bytes.
Sourcepub const fn from_le_bytes(bytes: [u8; 8]) -> Self
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self
Reconstructs a Decimal from its little-endian byte representation.
Sourcepub const fn from_be_bytes(bytes: [u8; 8]) -> Self
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self
Reconstructs a Decimal from its big-endian byte representation.
Sourcepub const fn from_ne_bytes(bytes: [u8; 8]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self
Reconstructs a Decimal from its native-endian byte representation.
Sourcepub fn write_le_bytes(&self, buf: &mut [u8])
pub fn write_le_bytes(&self, buf: &mut [u8])
Writes little-endian bytes into buf. Panics if buf.len() < 8.
Sourcepub fn write_be_bytes(&self, buf: &mut [u8])
pub fn write_be_bytes(&self, buf: &mut [u8])
Writes big-endian bytes into buf. Panics if buf.len() < 8.
Sourcepub fn read_le_bytes(buf: &[u8]) -> Self
pub fn read_le_bytes(buf: &[u8]) -> Self
Reads little-endian bytes from buf. Panics if buf.len() < 8.
Sourcepub fn read_be_bytes(buf: &[u8]) -> Self
pub fn read_be_bytes(buf: &[u8]) -> Self
Reads big-endian bytes from buf. Panics if buf.len() < 8.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const fn to_le_bytes(self) -> [u8; 16]
pub const fn to_le_bytes(self) -> [u8; 16]
Returns the underlying value as little-endian bytes.
Sourcepub const fn to_be_bytes(self) -> [u8; 16]
pub const fn to_be_bytes(self) -> [u8; 16]
Returns the underlying value as big-endian bytes.
Sourcepub const fn to_ne_bytes(self) -> [u8; 16]
pub const fn to_ne_bytes(self) -> [u8; 16]
Returns the underlying value as native-endian bytes.
Sourcepub const fn from_le_bytes(bytes: [u8; 16]) -> Self
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self
Reconstructs a Decimal from its little-endian byte representation.
Sourcepub const fn from_be_bytes(bytes: [u8; 16]) -> Self
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self
Reconstructs a Decimal from its big-endian byte representation.
Sourcepub const fn from_ne_bytes(bytes: [u8; 16]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self
Reconstructs a Decimal from its native-endian byte representation.
Sourcepub fn write_le_bytes(&self, buf: &mut [u8])
pub fn write_le_bytes(&self, buf: &mut [u8])
Writes little-endian bytes into buf. Panics if buf.len() < 16.
Sourcepub fn write_be_bytes(&self, buf: &mut [u8])
pub fn write_be_bytes(&self, buf: &mut [u8])
Writes big-endian bytes into buf. Panics if buf.len() < 16.
Sourcepub fn read_le_bytes(buf: &[u8]) -> Self
pub fn read_le_bytes(buf: &[u8]) -> Self
Reads little-endian bytes from buf. Panics if buf.len() < 16.
Sourcepub fn read_be_bytes(buf: &[u8]) -> Self
pub fn read_be_bytes(buf: &[u8]) -> Self
Reads big-endian bytes from buf. Panics if buf.len() < 16.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const EPSILON: Self
pub const EPSILON: Self
Smallest positive representable value (from_raw(1)).
Represents 1 / 10^D — the resolution of this decimal type.
Sourcepub const HALF: Self
pub const HALF: Self
One half (0.5).
§Compile-time constraint
Requires D >= 1. Referencing HALF on a Decimal with
D = 0 is a compile error — the value 0.5 is not
representable with zero fractional digits.
Sourcepub const BASIS_POINT: Self
pub const BASIS_POINT: Self
One basis point (0.0001).
§Compile-time constraint
Requires D >= 4. Referencing BASIS_POINT on a Decimal
with fewer than 4 fractional digits is a compile error.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const EPSILON: Self
pub const EPSILON: Self
Smallest positive representable value (from_raw(1)).
Represents 1 / 10^D — the resolution of this decimal type.
Sourcepub const HALF: Self
pub const HALF: Self
One half (0.5).
§Compile-time constraint
Requires D >= 1. Referencing HALF on a Decimal with
D = 0 is a compile error — the value 0.5 is not
representable with zero fractional digits.
Sourcepub const BASIS_POINT: Self
pub const BASIS_POINT: Self
One basis point (0.0001).
§Compile-time constraint
Requires D >= 4. Referencing BASIS_POINT on a Decimal
with fewer than 4 fractional digits is a compile error.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const EPSILON: Self
pub const EPSILON: Self
Smallest positive representable value (from_raw(1)).
Represents 1 / 10^D — the resolution of this decimal type.
Sourcepub const HALF: Self
pub const HALF: Self
One half (0.5).
§Compile-time constraint
Requires D >= 1. Referencing HALF on a Decimal with
D = 0 is a compile error — the value 0.5 is not
representable with zero fractional digits.
Sourcepub const BASIS_POINT: Self
pub const BASIS_POINT: Self
One basis point (0.0001).
§Compile-time constraint
Requires D >= 4. Referencing BASIS_POINT on a Decimal
with fewer than 4 fractional digits is a compile error.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub fn from_str_exact(s: &str) -> Result<Self, ParseError>
pub fn from_str_exact(s: &str) -> Result<Self, ParseError>
Parses a decimal string exactly. Rejects inputs with more
fractional digits than DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::from_str_exact("123.45").unwrap();
assert_eq!(price, D64::new(123, 45_000_000));Sourcepub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
pub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
Parses a decimal string, rounding excess precision using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Input has 10 decimal places, D64 has 8 — rounds
let price = D64::from_str_lossy("1.2345678951").unwrap();
assert_eq!(price, D64::new(1, 23_456_790)); // rounded upSourcepub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
pub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
Parses from a UTF-8 byte slice.
Sourcepub const fn from_i32(value: i32) -> Option<Self>
pub const fn from_i32(value: i32) -> Option<Self>
Creates a Decimal from an i32. Returns None on overflow.
Sourcepub const fn from_i64(value: i64) -> Option<Self>
pub const fn from_i64(value: i64) -> Option<Self>
Creates a Decimal from an i64. Returns None on overflow.
Sourcepub const fn from_u32(value: u32) -> Option<Self>
pub const fn from_u32(value: u32) -> Option<Self>
Creates a Decimal from a u32. Returns None on overflow.
Sourcepub const fn from_u64(value: u64) -> Option<Self>
pub const fn from_u64(value: u64) -> Option<Self>
Creates a Decimal from a u64. Returns None on overflow.
Sourcepub const fn from_scaled(value: i32, scale: u8) -> Option<Self>
pub const fn from_scaled(value: i32, scale: u8) -> Option<Self>
Constructs a Decimal representing value * 10^-scale.
Useful for tick sizes and precision-boundary construction.
For example, from_scaled(1, 5) returns a value equal to
0.00001.
Returns None if:
scale > D(would require rounding; usefrom_str_lossyfor a rounding policy)- The scaled value overflows the backing type
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let tick = D64::from_scaled(1, 5).unwrap();
assert_eq!(tick, D64::from_str_exact("0.00001").unwrap());
// scale > D
assert!(D64::from_scaled(1, 9).is_none());Sourcepub fn from_f64(value: f64) -> Result<Self, ConvertError>
pub fn from_f64(value: f64) -> Result<Self, ConvertError>
Creates a Decimal from an f64. Returns error on NaN, Inf, or overflow.
Requires the std feature (uses f64::round()).
Sourcepub fn from_f32(value: f32) -> Result<Self, ConvertError>
pub fn from_f32(value: f32) -> Result<Self, ConvertError>
Creates a Decimal from an f32.
Requires the std feature (uses f64::round()).
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub fn from_str_exact(s: &str) -> Result<Self, ParseError>
pub fn from_str_exact(s: &str) -> Result<Self, ParseError>
Parses a decimal string exactly. Rejects inputs with more
fractional digits than DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::from_str_exact("123.45").unwrap();
assert_eq!(price, D64::new(123, 45_000_000));Sourcepub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
pub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
Parses a decimal string, rounding excess precision using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Input has 10 decimal places, D64 has 8 — rounds
let price = D64::from_str_lossy("1.2345678951").unwrap();
assert_eq!(price, D64::new(1, 23_456_790)); // rounded upSourcepub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
pub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
Parses from a UTF-8 byte slice.
Sourcepub const fn from_i32(value: i32) -> Option<Self>
pub const fn from_i32(value: i32) -> Option<Self>
Creates a Decimal from an i32. Returns None on overflow.
Sourcepub const fn from_i64(value: i64) -> Option<Self>
pub const fn from_i64(value: i64) -> Option<Self>
Creates a Decimal from an i64. Returns None on overflow.
Sourcepub const fn from_u32(value: u32) -> Option<Self>
pub const fn from_u32(value: u32) -> Option<Self>
Creates a Decimal from a u32. Returns None on overflow.
Sourcepub const fn from_u64(value: u64) -> Option<Self>
pub const fn from_u64(value: u64) -> Option<Self>
Creates a Decimal from a u64. Returns None on overflow.
Sourcepub const fn from_scaled(value: i64, scale: u8) -> Option<Self>
pub const fn from_scaled(value: i64, scale: u8) -> Option<Self>
Constructs a Decimal representing value * 10^-scale.
Useful for tick sizes and precision-boundary construction.
For example, from_scaled(1, 5) returns a value equal to
0.00001.
Returns None if:
scale > D(would require rounding; usefrom_str_lossyfor a rounding policy)- The scaled value overflows the backing type
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let tick = D64::from_scaled(1, 5).unwrap();
assert_eq!(tick, D64::from_str_exact("0.00001").unwrap());
// scale > D
assert!(D64::from_scaled(1, 9).is_none());Sourcepub fn from_f64(value: f64) -> Result<Self, ConvertError>
pub fn from_f64(value: f64) -> Result<Self, ConvertError>
Creates a Decimal from an f64. Returns error on NaN, Inf, or overflow.
Requires the std feature (uses f64::round()).
Sourcepub fn from_f32(value: f32) -> Result<Self, ConvertError>
pub fn from_f32(value: f32) -> Result<Self, ConvertError>
Creates a Decimal from an f32.
Requires the std feature (uses f64::round()).
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub fn from_str_exact(s: &str) -> Result<Self, ParseError>
pub fn from_str_exact(s: &str) -> Result<Self, ParseError>
Parses a decimal string exactly. Rejects inputs with more
fractional digits than DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::from_str_exact("123.45").unwrap();
assert_eq!(price, D64::new(123, 45_000_000));Sourcepub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
pub fn from_str_lossy(s: &str) -> Result<Self, ParseError>
Parses a decimal string, rounding excess precision using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Input has 10 decimal places, D64 has 8 — rounds
let price = D64::from_str_lossy("1.2345678951").unwrap();
assert_eq!(price, D64::new(1, 23_456_790)); // rounded upSourcepub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
pub fn from_utf8_bytes(bytes: &[u8]) -> Result<Self, ParseError>
Parses from a UTF-8 byte slice.
Sourcepub const fn from_i32(value: i32) -> Option<Self>
pub const fn from_i32(value: i32) -> Option<Self>
Creates a Decimal from an i32. Returns None on overflow.
Sourcepub const fn from_i64(value: i64) -> Option<Self>
pub const fn from_i64(value: i64) -> Option<Self>
Creates a Decimal from an i64. Returns None on overflow.
Sourcepub const fn from_u32(value: u32) -> Option<Self>
pub const fn from_u32(value: u32) -> Option<Self>
Creates a Decimal from a u32. Returns None on overflow.
Sourcepub const fn from_u64(value: u64) -> Option<Self>
pub const fn from_u64(value: u64) -> Option<Self>
Creates a Decimal from a u64. Returns None on overflow.
Sourcepub const fn from_scaled(value: i128, scale: u8) -> Option<Self>
pub const fn from_scaled(value: i128, scale: u8) -> Option<Self>
Constructs a Decimal representing value * 10^-scale.
Useful for tick sizes and precision-boundary construction.
For example, from_scaled(1, 5) returns a value equal to
0.00001.
Returns None if:
scale > D(would require rounding; usefrom_str_lossyfor a rounding policy)- The scaled value overflows the backing type
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let tick = D64::from_scaled(1, 5).unwrap();
assert_eq!(tick, D64::from_str_exact("0.00001").unwrap());
// scale > D
assert!(D64::from_scaled(1, 9).is_none());Sourcepub fn from_f64(value: f64) -> Result<Self, ConvertError>
pub fn from_f64(value: f64) -> Result<Self, ConvertError>
Creates a Decimal from an f64. Returns error on NaN, Inf, or overflow.
Requires the std feature (uses f64::round()).
Sourcepub fn from_f32(value: f32) -> Result<Self, ConvertError>
pub fn from_f32(value: f32) -> Result<Self, ConvertError>
Creates a Decimal from an f32.
Requires the std feature (uses f64::round()).
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const SCALE: i32
pub const SCALE: i32
The scale factor 10^DECIMALS.
Validated at compile time: panics if DECIMALS is too
large for the backing type.
Sourcepub const fn from_raw(value: i32) -> Self
pub const fn from_raw(value: i32) -> Self
Creates a Decimal from a raw pre-scaled value.
No validation — the caller is responsible for ensuring the value is in the expected scale.
Sourcepub const fn new(integer: i32, fractional: i32) -> Self
pub const fn new(integer: i32, fractional: i32) -> Self
Creates a Decimal from integer and fractional parts.
The fractional part is combined with the integer part as
integer * SCALE + fractional. For conventional usage,
pass a non-negative fractional less than SCALE.
For negative values, negate the integer part:
new(-123, 45_000_000) → -123.45 (for DECIMALS=8).
§Panics
Panics if the result overflows the backing type.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
const PRICE: D64 = D64::new(100, 50_000_000); // 100.50
const NEG: D64 = D64::new(-50, 25_000_000); // -50.25Sourcepub const fn from_parts(
integer: i32,
fractional: i32,
negative: bool,
) -> Option<Self>
pub const fn from_parts( integer: i32, fractional: i32, negative: bool, ) -> Option<Self>
Construct from integer part, fractional part, and sign.
The fractional part is always positive (represents digits
after the decimal point). Use negative to control sign.
This handles the -0.5 case that new() cannot express
(because -0 == 0 for integers).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let neg_half = D64::from_parts(0, 50_000_000, true);
assert_eq!(neg_half.unwrap().to_raw(), -50_000_000);
let pos = D64::from_parts(1, 25_000_000, false);
assert_eq!(pos.unwrap().to_raw(), 125_000_000);Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if the value is strictly positive.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if the value is strictly negative.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const SCALE: i64
pub const SCALE: i64
The scale factor 10^DECIMALS.
Validated at compile time: panics if DECIMALS is too
large for the backing type.
Sourcepub const fn from_raw(value: i64) -> Self
pub const fn from_raw(value: i64) -> Self
Creates a Decimal from a raw pre-scaled value.
No validation — the caller is responsible for ensuring the value is in the expected scale.
Sourcepub const fn new(integer: i64, fractional: i64) -> Self
pub const fn new(integer: i64, fractional: i64) -> Self
Creates a Decimal from integer and fractional parts.
The fractional part is combined with the integer part as
integer * SCALE + fractional. For conventional usage,
pass a non-negative fractional less than SCALE.
For negative values, negate the integer part:
new(-123, 45_000_000) → -123.45 (for DECIMALS=8).
§Panics
Panics if the result overflows the backing type.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
const PRICE: D64 = D64::new(100, 50_000_000); // 100.50
const NEG: D64 = D64::new(-50, 25_000_000); // -50.25Sourcepub const fn from_parts(
integer: i64,
fractional: i64,
negative: bool,
) -> Option<Self>
pub const fn from_parts( integer: i64, fractional: i64, negative: bool, ) -> Option<Self>
Construct from integer part, fractional part, and sign.
The fractional part is always positive (represents digits
after the decimal point). Use negative to control sign.
This handles the -0.5 case that new() cannot express
(because -0 == 0 for integers).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let neg_half = D64::from_parts(0, 50_000_000, true);
assert_eq!(neg_half.unwrap().to_raw(), -50_000_000);
let pos = D64::from_parts(1, 25_000_000, false);
assert_eq!(pos.unwrap().to_raw(), 125_000_000);Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if the value is strictly positive.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if the value is strictly negative.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const SCALE: i128
pub const SCALE: i128
The scale factor 10^DECIMALS.
Validated at compile time: panics if DECIMALS is too
large for the backing type.
Sourcepub const fn from_raw(value: i128) -> Self
pub const fn from_raw(value: i128) -> Self
Creates a Decimal from a raw pre-scaled value.
No validation — the caller is responsible for ensuring the value is in the expected scale.
Sourcepub const fn new(integer: i128, fractional: i128) -> Self
pub const fn new(integer: i128, fractional: i128) -> Self
Creates a Decimal from integer and fractional parts.
The fractional part is combined with the integer part as
integer * SCALE + fractional. For conventional usage,
pass a non-negative fractional less than SCALE.
For negative values, negate the integer part:
new(-123, 45_000_000) → -123.45 (for DECIMALS=8).
§Panics
Panics if the result overflows the backing type.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
const PRICE: D64 = D64::new(100, 50_000_000); // 100.50
const NEG: D64 = D64::new(-50, 25_000_000); // -50.25Sourcepub const fn from_parts(
integer: i128,
fractional: i128,
negative: bool,
) -> Option<Self>
pub const fn from_parts( integer: i128, fractional: i128, negative: bool, ) -> Option<Self>
Construct from integer part, fractional part, and sign.
The fractional part is always positive (represents digits
after the decimal point). Use negative to control sign.
This handles the -0.5 case that new() cannot express
(because -0 == 0 for integers).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let neg_half = D64::from_parts(0, 50_000_000, true);
assert_eq!(neg_half.unwrap().to_raw(), -50_000_000);
let pos = D64::from_parts(1, 25_000_000, false);
assert_eq!(pos.unwrap().to_raw(), 125_000_000);Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if the value is strictly positive.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if the value is strictly negative.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn midpoint(self, other: Self) -> Self
pub const fn midpoint(self, other: Self) -> Self
Midpoint of two prices: (self + other) / 2.
Overflow-safe midpoint: (self + other) / 2.
Uses the bit-manipulation formula (a & b) + ((a ^ b) >> 1)
which is correct for all representable values without
intermediate overflow.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let bid = D64::new(100, 0);
let ask = D64::new(101, 0);
assert_eq!(bid.midpoint(ask), D64::new(100, 50_000_000));Sourcepub const fn spread(self, other: Self) -> Option<Self>
pub const fn spread(self, other: Self) -> Option<Self>
Spread between two prices: self - other.
Returns None if self < other (crossed market).
Sourcepub const fn round_to_tick(self, tick: Self) -> Option<Self>
pub const fn round_to_tick(self, tick: Self) -> Option<Self>
Round to nearest tick size.
tick must be positive. Rounds to the nearest multiple
of tick using banker’s rounding on the remainder.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_700_000); // 1.237
let tick = D64::new(0, 5_000_000); // 0.05
assert_eq!(price.round_to_tick(tick), Some(D64::new(1, 25_000_000))); // 1.25Sourcepub const fn floor_to_tick(self, tick: Self) -> Option<Self>
pub const fn floor_to_tick(self, tick: Self) -> Option<Self>
Floor to tick: round down to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
pub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
Ceil to tick: round up to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn halve(self) -> Self
pub const fn halve(self) -> Self
Divide by 2 using integer division. Truncates toward zero.
The compiler optimizes this to a shift + sign-bit adjustment.
Sourcepub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
pub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
Returns true if self is within tolerance of other.
Equivalent to |self - other| <= tolerance. Returns false
when the difference overflows (values with opposite signs
near MAX/MIN).
Sourcepub const fn clamp_price(self, min: Self, max: Self) -> Self
pub const fn clamp_price(self, min: Self, max: Self) -> Self
Clamp to a price range [min, max].
Sourcepub const fn is_tick_aligned(self, tick: Self) -> bool
pub const fn is_tick_aligned(self, tick: Self) -> bool
Returns true if self is aligned to the given tick size.
Panics if tick is not positive.
Sourcepub const fn round_bps(self, n: u32) -> Option<Self>
pub const fn round_bps(self, n: u32) -> Option<Self>
Round to nearest N basis points.
Returns None if n == 0 or the tick computation overflows.
§Compile-time constraint
Requires D >= 4. Referencing this method on a Decimal
with D < 4 is a compile error.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn midpoint(self, other: Self) -> Self
pub const fn midpoint(self, other: Self) -> Self
Midpoint of two prices: (self + other) / 2.
Overflow-safe midpoint: (self + other) / 2.
Uses the bit-manipulation formula (a & b) + ((a ^ b) >> 1)
which is correct for all representable values without
intermediate overflow.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let bid = D64::new(100, 0);
let ask = D64::new(101, 0);
assert_eq!(bid.midpoint(ask), D64::new(100, 50_000_000));Sourcepub const fn spread(self, other: Self) -> Option<Self>
pub const fn spread(self, other: Self) -> Option<Self>
Spread between two prices: self - other.
Returns None if self < other (crossed market).
Sourcepub const fn round_to_tick(self, tick: Self) -> Option<Self>
pub const fn round_to_tick(self, tick: Self) -> Option<Self>
Round to nearest tick size.
tick must be positive. Rounds to the nearest multiple
of tick using banker’s rounding on the remainder.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_700_000); // 1.237
let tick = D64::new(0, 5_000_000); // 0.05
assert_eq!(price.round_to_tick(tick), Some(D64::new(1, 25_000_000))); // 1.25Sourcepub const fn floor_to_tick(self, tick: Self) -> Option<Self>
pub const fn floor_to_tick(self, tick: Self) -> Option<Self>
Floor to tick: round down to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
pub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
Ceil to tick: round up to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn halve(self) -> Self
pub const fn halve(self) -> Self
Divide by 2 using integer division. Truncates toward zero.
The compiler optimizes this to a shift + sign-bit adjustment.
Sourcepub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
pub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
Returns true if self is within tolerance of other.
Equivalent to |self - other| <= tolerance. Returns false
when the difference overflows (values with opposite signs
near MAX/MIN).
Sourcepub const fn clamp_price(self, min: Self, max: Self) -> Self
pub const fn clamp_price(self, min: Self, max: Self) -> Self
Clamp to a price range [min, max].
Sourcepub const fn is_tick_aligned(self, tick: Self) -> bool
pub const fn is_tick_aligned(self, tick: Self) -> bool
Returns true if self is aligned to the given tick size.
Panics if tick is not positive.
Sourcepub const fn round_bps(self, n: u32) -> Option<Self>
pub const fn round_bps(self, n: u32) -> Option<Self>
Round to nearest N basis points.
Returns None if n == 0 or the tick computation overflows.
§Compile-time constraint
Requires D >= 4. Referencing this method on a Decimal
with D < 4 is a compile error.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const fn midpoint(self, other: Self) -> Self
pub const fn midpoint(self, other: Self) -> Self
Midpoint of two prices: (self + other) / 2.
Overflow-safe midpoint: (self + other) / 2.
Uses the bit-manipulation formula (a & b) + ((a ^ b) >> 1)
which is correct for all representable values without
intermediate overflow.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let bid = D64::new(100, 0);
let ask = D64::new(101, 0);
assert_eq!(bid.midpoint(ask), D64::new(100, 50_000_000));Sourcepub const fn spread(self, other: Self) -> Option<Self>
pub const fn spread(self, other: Self) -> Option<Self>
Spread between two prices: self - other.
Returns None if self < other (crossed market).
Sourcepub const fn round_to_tick(self, tick: Self) -> Option<Self>
pub const fn round_to_tick(self, tick: Self) -> Option<Self>
Round to nearest tick size.
tick must be positive. Rounds to the nearest multiple
of tick using banker’s rounding on the remainder.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_700_000); // 1.237
let tick = D64::new(0, 5_000_000); // 0.05
assert_eq!(price.round_to_tick(tick), Some(D64::new(1, 25_000_000))); // 1.25Sourcepub const fn floor_to_tick(self, tick: Self) -> Option<Self>
pub const fn floor_to_tick(self, tick: Self) -> Option<Self>
Floor to tick: round down to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
pub const fn ceil_to_tick(self, tick: Self) -> Option<Self>
Ceil to tick: round up to nearest multiple of tick.
Returns None if the result would overflow.
Sourcepub const fn halve(self) -> Self
pub const fn halve(self) -> Self
Divide by 2 using integer division. Truncates toward zero.
The compiler optimizes this to a shift + sign-bit adjustment.
Sourcepub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
pub const fn approx_eq(self, other: Self, tolerance: Self) -> bool
Returns true if self is within tolerance of other.
Equivalent to |self - other| <= tolerance. Returns false
when the difference overflows (values with opposite signs
near MAX/MIN).
Sourcepub const fn clamp_price(self, min: Self, max: Self) -> Self
pub const fn clamp_price(self, min: Self, max: Self) -> Self
Clamp to a price range [min, max].
Sourcepub const fn is_tick_aligned(self, tick: Self) -> bool
pub const fn is_tick_aligned(self, tick: Self) -> bool
Returns true if self is aligned to the given tick size.
Panics if tick is not positive.
Sourcepub const fn round_bps(self, n: u32) -> Option<Self>
pub const fn round_bps(self, n: u32) -> Option<Self>
Round to nearest N basis points.
Returns None if n == 0 or the tick computation overflows.
§Compile-time constraint
Requires D >= 4. Referencing this method on a Decimal
with D < 4 is a compile error.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn percent_of(self, percent: Self) -> Option<Self>
pub const fn percent_of(self, percent: Self) -> Option<Self>
Compute self * percent / 100 via single truncating division.
percent is in percentage points: 50 means 50%.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn percent_of(self, percent: Self) -> Option<Self>
pub const fn percent_of(self, percent: Self) -> Option<Self>
Compute self * percent / 100 via single truncating division.
percent is in percentage points: 50 means 50%.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn bps_of(self, bps: i32) -> Option<Self>
pub const fn bps_of(self, bps: i32) -> Option<Self>
N basis points of self: self * bps / 10000.
Sourcepub const fn shift_bps(self, bps: i32) -> Option<Self>
pub const fn shift_bps(self, bps: i32) -> Option<Self>
Adjust self by N basis points: self * (10000 + bps) / 10000.
Sourcepub const fn shift_pct(self, pct: i32) -> Option<Self>
pub const fn shift_pct(self, pct: i32) -> Option<Self>
Adjust self by N percent: self * (100 + pct) / 100.
Sourcepub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Difference in bps relative to divisor:
(self - other) / divisor * 10000, returned as a Decimal.
Uses multiply-before-divide: diff * SCALE / divisor * 10000
to preserve precision through the fixed-point division.
Sourcepub const fn bps_diff(self, other: Self) -> Option<Self>
pub const fn bps_diff(self, other: Self) -> Option<Self>
Difference in bps: (self - other) * 10000 / other.
Sourcepub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Percentage difference relative to divisor:
(self - other) / divisor * 100, returned as a Decimal.
Sourcepub const fn pct_diff(self, other: Self) -> Option<Self>
pub const fn pct_diff(self, other: Self) -> Option<Self>
Percentage difference: (self - other) * 100 / other.
Sourcepub const fn within_bps(self, other: Self, bps: i32) -> bool
pub const fn within_bps(self, other: Self, bps: i32) -> bool
Returns true if |self - other| <= |other| * bps / 10000.
Sourcepub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
pub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
Returns true if |self - other| <= n * tick.
Panics if tick is not positive.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn bps_of(self, bps: i32) -> Option<Self>
pub const fn bps_of(self, bps: i32) -> Option<Self>
N basis points of self: self * bps / 10000.
Sourcepub const fn shift_bps(self, bps: i32) -> Option<Self>
pub const fn shift_bps(self, bps: i32) -> Option<Self>
Adjust self by N basis points: self * (10000 + bps) / 10000.
Sourcepub const fn shift_pct(self, pct: i32) -> Option<Self>
pub const fn shift_pct(self, pct: i32) -> Option<Self>
Adjust self by N percent: self * (100 + pct) / 100.
Sourcepub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Difference in bps relative to divisor:
(self - other) / divisor * 10000, returned as a Decimal.
Uses multiply-before-divide: diff * SCALE / divisor * 10000
to preserve precision through the fixed-point division.
Sourcepub const fn bps_diff(self, other: Self) -> Option<Self>
pub const fn bps_diff(self, other: Self) -> Option<Self>
Difference in bps: (self - other) * 10000 / other.
Sourcepub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Percentage difference relative to divisor:
(self - other) / divisor * 100, returned as a Decimal.
Sourcepub const fn pct_diff(self, other: Self) -> Option<Self>
pub const fn pct_diff(self, other: Self) -> Option<Self>
Percentage difference: (self - other) * 100 / other.
Sourcepub const fn within_bps(self, other: Self, bps: i32) -> bool
pub const fn within_bps(self, other: Self, bps: i32) -> bool
Returns true if |self - other| <= |other| * bps / 10000.
Sourcepub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
pub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
Returns true if |self - other| <= n * tick.
Panics if tick is not positive.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub fn mul_div(self, mul: Self, div: Self) -> Option<Self>
pub fn mul_div(self, mul: Self, div: Self) -> Option<Self>
Fused multiply-divide: (self * a) / b with single rounding.
For i128, delegates to checked_mul then checked_div. Not truly fused (two rounding events) — a 256-bit intermediate would be needed for true single-rounding on i128.
Sourcepub const fn bps_of(self, bps: i32) -> Option<Self>
pub const fn bps_of(self, bps: i32) -> Option<Self>
N basis points of self: self * bps / 10000.
Uses decomposition to avoid intermediate overflow.
Sourcepub const fn pct_of(self, pct: i32) -> Option<Self>
pub const fn pct_of(self, pct: i32) -> Option<Self>
N percent of self: self * pct / 100.
Uses decomposition to avoid intermediate overflow.
Sourcepub const fn shift_bps(self, bps: i32) -> Option<Self>
pub const fn shift_bps(self, bps: i32) -> Option<Self>
Adjust self by N basis points: self * (10000 + bps) / 10000.
Sourcepub const fn shift_pct(self, pct: i32) -> Option<Self>
pub const fn shift_pct(self, pct: i32) -> Option<Self>
Adjust self by N percent: self * (100 + pct) / 100.
Sourcepub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn bps_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Difference in bps relative to divisor:
(self - other) / divisor * 10000, returned as a Decimal.
Decomposes diff = q*divisor + r, then computes
q * SCALE * 10000 + r * SCALE * 10000 / divisor
to avoid intermediate overflow on i128.
Sourcepub const fn bps_diff(self, other: Self) -> Option<Self>
pub const fn bps_diff(self, other: Self) -> Option<Self>
Difference in bps: (self - other) * 10000 / other.
Sourcepub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
pub const fn pct_diff_by(self, other: Self, divisor: Self) -> Option<Self>
Percentage difference relative to divisor:
(self - other) / divisor * 100, returned as a Decimal.
Sourcepub const fn pct_diff(self, other: Self) -> Option<Self>
pub const fn pct_diff(self, other: Self) -> Option<Self>
Percentage difference: (self - other) * 100 / other.
Sourcepub const fn within_bps(self, other: Self, bps: i32) -> bool
pub const fn within_bps(self, other: Self, bps: i32) -> bool
Returns true if |self - other| <= |other| * bps / 10000.
Sourcepub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
pub const fn within_ticks(self, other: Self, n: i64, tick: Self) -> bool
Returns true if |self - other| <= n * tick.
Panics if tick is not positive.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub fn write_to_buf(&self, buf: &mut [u8]) -> usize
pub fn write_to_buf(&self, buf: &mut [u8]) -> usize
Write decimal representation to a byte buffer.
Returns the number of bytes written. Buffer must be at
least 64 bytes. Useful for wire protocol encoding without
fmt overhead.
Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub fn write_to_buf(&self, buf: &mut [u8]) -> usize
pub fn write_to_buf(&self, buf: &mut [u8]) -> usize
Write decimal representation to a byte buffer.
Returns the number of bytes written. Buffer must be at
least 64 bytes. Useful for wire protocol encoding without
fmt overhead.
Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub fn write_to_buf(&self, buf: &mut [u8]) -> usize
pub fn write_to_buf(&self, buf: &mut [u8]) -> usize
Write decimal representation to a byte buffer.
Returns the number of bytes written. Buffer must be at
least 64 bytes. Useful for wire protocol encoding without
fmt overhead.
Source§impl<const D: u8> Decimal<i32, D>
impl<const D: u8> Decimal<i32, D>
Sourcepub const fn floor(self) -> Self
pub const fn floor(self) -> Self
Rounds toward negative infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 75_000_000); // 1.75
assert_eq!(pos.floor().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 75_000_000); // -1.75
assert_eq!(neg.floor().to_raw(), D64::new(-2, 0).to_raw());Sourcepub const fn ceil(self) -> Self
pub const fn ceil(self) -> Self
Rounds toward positive infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 25_000_000); // 1.25
assert_eq!(pos.ceil().to_raw(), D64::new(2, 0).to_raw());
let neg = D64::new(-1, 25_000_000); // -1.25
assert_eq!(neg.ceil().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn trunc(self) -> Self
pub const fn trunc(self) -> Self
Truncates toward zero (removes fractional part).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 99_000_000); // 1.99
assert_eq!(pos.trunc().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 99_000_000); // -1.99
assert_eq!(neg.trunc().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn fract(self) -> Self
pub const fn fract(self) -> Self
Returns the fractional part (same sign as self).
Invariant: self == self.trunc() + self.fract().
Sourcepub const fn to_integer(self) -> i32
pub const fn to_integer(self) -> i32
Returns the integer part as the backing type.
Equivalent to self.trunc().to_raw() / SCALE.
Sourcepub const fn round(self) -> Self
pub const fn round(self) -> Self
Rounds to the nearest integer using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Half rounds to even
let half_even = D64::new(2, 50_000_000); // 2.5
assert_eq!(half_even.round().to_raw(), D64::new(2, 0).to_raw());
let half_odd = D64::new(3, 50_000_000); // 3.5
assert_eq!(half_odd.round().to_raw(), D64::new(4, 0).to_raw());Sourcepub const fn round_dp(self, dp: u8) -> Self
pub const fn round_dp(self, dp: u8) -> Self
Rounds to dp decimal places using banker’s rounding.
§Panics
Panics if dp >= DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_456_789); // 1.23456789
let rounded = price.round_dp(2); // 1.23
assert_eq!(rounded.to_raw(), D64::new(1, 23_000_000).to_raw());Source§impl<const D: u8> Decimal<i64, D>
impl<const D: u8> Decimal<i64, D>
Sourcepub const fn floor(self) -> Self
pub const fn floor(self) -> Self
Rounds toward negative infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 75_000_000); // 1.75
assert_eq!(pos.floor().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 75_000_000); // -1.75
assert_eq!(neg.floor().to_raw(), D64::new(-2, 0).to_raw());Sourcepub const fn ceil(self) -> Self
pub const fn ceil(self) -> Self
Rounds toward positive infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 25_000_000); // 1.25
assert_eq!(pos.ceil().to_raw(), D64::new(2, 0).to_raw());
let neg = D64::new(-1, 25_000_000); // -1.25
assert_eq!(neg.ceil().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn trunc(self) -> Self
pub const fn trunc(self) -> Self
Truncates toward zero (removes fractional part).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 99_000_000); // 1.99
assert_eq!(pos.trunc().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 99_000_000); // -1.99
assert_eq!(neg.trunc().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn fract(self) -> Self
pub const fn fract(self) -> Self
Returns the fractional part (same sign as self).
Invariant: self == self.trunc() + self.fract().
Sourcepub const fn to_integer(self) -> i64
pub const fn to_integer(self) -> i64
Returns the integer part as the backing type.
Equivalent to self.trunc().to_raw() / SCALE.
Sourcepub const fn round(self) -> Self
pub const fn round(self) -> Self
Rounds to the nearest integer using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Half rounds to even
let half_even = D64::new(2, 50_000_000); // 2.5
assert_eq!(half_even.round().to_raw(), D64::new(2, 0).to_raw());
let half_odd = D64::new(3, 50_000_000); // 3.5
assert_eq!(half_odd.round().to_raw(), D64::new(4, 0).to_raw());Sourcepub const fn round_dp(self, dp: u8) -> Self
pub const fn round_dp(self, dp: u8) -> Self
Rounds to dp decimal places using banker’s rounding.
§Panics
Panics if dp >= DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_456_789); // 1.23456789
let rounded = price.round_dp(2); // 1.23
assert_eq!(rounded.to_raw(), D64::new(1, 23_000_000).to_raw());Source§impl<const D: u8> Decimal<i128, D>
impl<const D: u8> Decimal<i128, D>
Sourcepub const fn floor(self) -> Self
pub const fn floor(self) -> Self
Rounds toward negative infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 75_000_000); // 1.75
assert_eq!(pos.floor().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 75_000_000); // -1.75
assert_eq!(neg.floor().to_raw(), D64::new(-2, 0).to_raw());Sourcepub const fn ceil(self) -> Self
pub const fn ceil(self) -> Self
Rounds toward positive infinity.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 25_000_000); // 1.25
assert_eq!(pos.ceil().to_raw(), D64::new(2, 0).to_raw());
let neg = D64::new(-1, 25_000_000); // -1.25
assert_eq!(neg.ceil().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn trunc(self) -> Self
pub const fn trunc(self) -> Self
Truncates toward zero (removes fractional part).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let pos = D64::new(1, 99_000_000); // 1.99
assert_eq!(pos.trunc().to_raw(), D64::new(1, 0).to_raw());
let neg = D64::new(-1, 99_000_000); // -1.99
assert_eq!(neg.trunc().to_raw(), D64::new(-1, 0).to_raw());Sourcepub const fn fract(self) -> Self
pub const fn fract(self) -> Self
Returns the fractional part (same sign as self).
Invariant: self == self.trunc() + self.fract().
Sourcepub const fn to_integer(self) -> i128
pub const fn to_integer(self) -> i128
Returns the integer part as the backing type.
Equivalent to self.trunc().to_raw() / SCALE.
Sourcepub const fn round(self) -> Self
pub const fn round(self) -> Self
Rounds to the nearest integer using banker’s rounding (round half to even).
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
// Half rounds to even
let half_even = D64::new(2, 50_000_000); // 2.5
assert_eq!(half_even.round().to_raw(), D64::new(2, 0).to_raw());
let half_odd = D64::new(3, 50_000_000); // 3.5
assert_eq!(half_odd.round().to_raw(), D64::new(4, 0).to_raw());Sourcepub const fn round_dp(self, dp: u8) -> Self
pub const fn round_dp(self, dp: u8) -> Self
Rounds to dp decimal places using banker’s rounding.
§Panics
Panics if dp >= DECIMALS.
§Examples
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
let price = D64::new(1, 23_456_789); // 1.23456789
let rounded = price.round_dp(2); // 1.23
assert_eq!(rounded.to_raw(), D64::new(1, 23_000_000).to_raw());Trait Implementations§
Source§impl<const D: u8> AddAssign for Decimal<i128, D>
impl<const D: u8> AddAssign for Decimal<i128, D>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<const D: u8> AddAssign for Decimal<i32, D>
impl<const D: u8> AddAssign for Decimal<i32, D>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<const D: u8> AddAssign for Decimal<i64, D>
impl<const D: u8> AddAssign for Decimal<i64, D>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<const D: u8> DivAssign for Decimal<i128, D>
impl<const D: u8> DivAssign for Decimal<i128, D>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<const D: u8> DivAssign for Decimal<i32, D>
impl<const D: u8> DivAssign for Decimal<i32, D>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<const D: u8> DivAssign for Decimal<i64, D>
impl<const D: u8> DivAssign for Decimal<i64, D>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<const D: u8> MulAssign for Decimal<i128, D>
impl<const D: u8> MulAssign for Decimal<i128, D>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<const D: u8> MulAssign for Decimal<i32, D>
impl<const D: u8> MulAssign for Decimal<i32, D>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<const D: u8> MulAssign for Decimal<i64, D>
impl<const D: u8> MulAssign for Decimal<i64, D>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<B: Ord + Backing, const DECIMALS: u8> Ord for Decimal<B, DECIMALS>
impl<B: Ord + Backing, const DECIMALS: u8> Ord for Decimal<B, DECIMALS>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<B: PartialEq + Backing, const DECIMALS: u8> PartialEq for Decimal<B, DECIMALS>
impl<B: PartialEq + Backing, const DECIMALS: u8> PartialEq for Decimal<B, DECIMALS>
Source§impl<B: PartialOrd + Backing, const DECIMALS: u8> PartialOrd for Decimal<B, DECIMALS>
impl<B: PartialOrd + Backing, const DECIMALS: u8> PartialOrd for Decimal<B, DECIMALS>
Source§impl<const D: u8> RemAssign for Decimal<i128, D>
impl<const D: u8> RemAssign for Decimal<i128, D>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<const D: u8> RemAssign for Decimal<i32, D>
impl<const D: u8> RemAssign for Decimal<i32, D>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<const D: u8> RemAssign for Decimal<i64, D>
impl<const D: u8> RemAssign for Decimal<i64, D>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<const D: u8> SubAssign for Decimal<i128, D>
impl<const D: u8> SubAssign for Decimal<i128, D>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<const D: u8> SubAssign for Decimal<i32, D>
impl<const D: u8> SubAssign for Decimal<i32, D>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<const D: u8> SubAssign for Decimal<i64, D>
impl<const D: u8> SubAssign for Decimal<i64, D>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more