pub struct Decimal { /* private fields */ }Expand description
A 128-bit signed fixed-point decimal.
value = mantissa × 10^(-scale)mantissa: signed coefficient stored asi128scale: number of decimal places in[0, MAX_SCALE]
On-chain Borsh encoding: 17 bytes (16-byte LE mantissa + 1-byte scale).
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub fn checked_add(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
pub fn checked_add(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
Checked addition. Aligns scales then adds mantissas.
Sourcepub fn checked_sub(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
pub fn checked_sub(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
Checked subtraction. Aligns scales then subtracts mantissas.
Sourcepub fn checked_mul(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
pub fn checked_mul(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
Checked multiplication: self * rhs.
Result scale = self.scale + rhs.scale.
Returns Err(ScaleExceeded) if that exceeds MAX_SCALE.
Sourcepub fn checked_div(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
pub fn checked_div(self, rhs: Decimal) -> Result<Decimal, ArithmeticError>
Checked division: self / rhs.
Scales the numerator up by MAX_SCALE - self.scale places before
dividing to retain maximum precision.
Sourcepub fn checked_neg(self) -> Result<Decimal, ArithmeticError>
pub fn checked_neg(self) -> Result<Decimal, ArithmeticError>
Negation: returns -self.
Fails with Err(Overflow) for Decimal::MIN (two’s-complement has no
positive counterpart for i128::MIN).
Sourcepub fn checked_abs(self) -> Result<Decimal, ArithmeticError>
pub fn checked_abs(self) -> Result<Decimal, ArithmeticError>
Absolute value: returns |self|.
Fails with Err(Overflow) for Decimal::MIN.
Sourcepub fn checked_mul_div(
self,
numerator: Decimal,
denominator: Decimal,
) -> Result<Decimal, ArithmeticError>
pub fn checked_mul_div( self, numerator: Decimal, denominator: Decimal, ) -> Result<Decimal, ArithmeticError>
Compound (self × numerator) / denominator with 256-bit intermediate.
Prevents silent overflow that occurs when self × numerator exceeds
i128::MAX in a naive two-step mul then div.
Source§impl Decimal
impl Decimal
Sourcepub fn from_u128(v: u128) -> Result<Self, ArithmeticError>
pub fn from_u128(v: u128) -> Result<Self, ArithmeticError>
Create a Decimal with scale 0 from a u128.
Returns Err(Overflow) if v > i128::MAX.
Sourcepub fn from_token_amount(
amount: u64,
decimals: u8,
) -> Result<Self, ArithmeticError>
pub fn from_token_amount( amount: u64, decimals: u8, ) -> Result<Self, ArithmeticError>
Create a Decimal from a raw SPL token amount and the mint’s decimals.
from_token_amount(1_500_000, 6) represents 1.500000 USDC.
Source§impl Decimal
impl Decimal
Sourcepub fn to_i128_truncated(self) -> i128
pub fn to_i128_truncated(self) -> i128
Truncate toward zero and return the integer part as i128.
Decimal { mantissa: 157, scale: 2 } (= 1.57) → 1
Sourcepub fn to_u64_truncated(self) -> Result<u64, ArithmeticError>
pub fn to_u64_truncated(self) -> Result<u64, ArithmeticError>
Truncate toward zero and return the integer part as u64.
Returns Err(Overflow) if the value is negative or exceeds u64::MAX.
Sourcepub fn to_token_amount(
self,
decimals: u8,
mode: RoundingMode,
) -> Result<u64, ArithmeticError>
pub fn to_token_amount( self, decimals: u8, mode: RoundingMode, ) -> Result<u64, ArithmeticError>
Convert to a raw SPL token u64 amount with explicit rounding.
Rounds to decimals decimal places first, then converts to an integer.
Source§impl Decimal
impl Decimal
Sourcepub fn from_str_exact(s: &str) -> Result<Self, ArithmeticError>
pub fn from_str_exact(s: &str) -> Result<Self, ArithmeticError>
Parse a decimal string into a Decimal.
Accepted formats:
"123"→{ mantissa: 123, scale: 0 }"1.23"→{ mantissa: 123, scale: 2 }"-1.23"→{ mantissa: -123, scale: 2 }"+1.23"→{ mantissa: 123, scale: 2 }
Returns Err(ScaleExceeded) if there are more than 28 fractional digits,
or Err(InvalidInput) for malformed strings.
Source§impl Decimal
impl Decimal
Sourcepub const TEN_THOUSAND: Self
pub const TEN_THOUSAND: Self
10_000 × 10^0 — basis-points denominator.
Sourcepub fn new(mantissa: i128, scale: u8) -> Result<Self, ArithmeticError>
pub fn new(mantissa: i128, scale: u8) -> Result<Self, ArithmeticError>
Construct a Decimal from a raw mantissa and scale.
Returns Err(ArithmeticError::ScaleExceeded) if scale > MAX_SCALE.
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Returns true if the value is strictly positive.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Returns true if the value is strictly negative.
Source§impl Decimal
impl Decimal
Sourcepub fn round(self, dp: u8, mode: RoundingMode) -> Result<Self, ArithmeticError>
pub fn round(self, dp: u8, mode: RoundingMode) -> Result<Self, ArithmeticError>
Round self to dp decimal places using the given rounding mode.
If dp >= self.scale no rounding is needed and self is returned
unchanged (possibly with a different scale representation).
§Errors
Returns Err(ScaleExceeded) if dp > MAX_SCALE.
Sourcepub fn rescale_up(self, new_scale: u8) -> Result<Self, ArithmeticError>
pub fn rescale_up(self, new_scale: u8) -> Result<Self, ArithmeticError>
Rescale self to a higher number of decimal places by padding zeros.
Only increases scale; use round to decrease it.
Returns Err(ScaleExceeded) if new_scale > MAX_SCALE or Err(Overflow)
if the mantissa multiplication overflows.