pub struct Amount { /* private fields */ }Expand description
A decimal-safe amount represented as integer minor units and a decimal scale.
Implementations§
Source§impl Amount
impl Amount
Sourcepub const fn from_minor_units(
minor_units: i128,
scale: u8,
) -> Result<Self, AmountError>
pub const fn from_minor_units( minor_units: i128, scale: u8, ) -> Result<Self, AmountError>
Creates an amount from integer minor units and a decimal scale.
§Errors
Returns AmountError::ScaleTooLarge when scale is greater than 18.
Sourcepub const fn zero(scale: u8) -> Result<Self, AmountError>
pub const fn zero(scale: u8) -> Result<Self, AmountError>
Creates a zero amount at the requested scale.
§Errors
Returns AmountError::ScaleTooLarge when scale is greater than 18.
Sourcepub const fn minor_units(self) -> i128
pub const fn minor_units(self) -> i128
Returns the integer minor-unit value.
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns whether this amount is greater than zero.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns whether this amount is less than zero.
Sourcepub const fn checked_abs(self) -> Result<Self, AmountError>
pub const fn checked_abs(self) -> Result<Self, AmountError>
Returns the absolute value of this amount.
§Errors
Returns AmountError::Overflow when the absolute value cannot be represented.
Sourcepub fn checked_add(self, other: Self) -> Result<Self, AmountError>
pub fn checked_add(self, other: Self) -> Result<Self, AmountError>
Adds two same-scale amounts.
§Errors
Returns AmountError::ScaleMismatch when scales differ and AmountError::Overflow
when the integer addition overflows.
Sourcepub fn checked_sub(self, other: Self) -> Result<Self, AmountError>
pub fn checked_sub(self, other: Self) -> Result<Self, AmountError>
Subtracts two same-scale amounts.
§Errors
Returns AmountError::ScaleMismatch when scales differ and AmountError::Overflow
when the integer subtraction overflows.
Sourcepub fn checked_rescale(self, new_scale: u8) -> Result<Self, AmountError>
pub fn checked_rescale(self, new_scale: u8) -> Result<Self, AmountError>
Rescales an amount without losing precision.
§Errors
Returns AmountError::ScaleTooLarge for unsupported scales,
AmountError::Overflow when scaling up overflows, and
AmountError::PrecisionLoss when scaling down would discard non-zero digits.