Struct dec::Decimal

source ·
#[repr(C)]
pub struct Decimal<const N: usize> { /* private fields */ }
Expand description

An arbitrary-precision decimal number.

The maximum number of digits that can be stored in the number is specified by N * 3. For example, a value of type Decimal<3> has space for nine decimal digits. This somewhat odd design is due to limitations of constant generic parameters in Rust. The intention is to someday make N correspond directly to the number of digits of precision.

N must be at least 12 and no greater than 999,999,999, though typically the stack size implies a smaller maximum for N. Due to limitations with constant generics it is not yet possible to enforce these restrictions at compile time, so they are checked at runtime.

For more details about e.g. the struct’s fields, see the upstream documentation.

Implementations§

source§

impl<const N: usize> Decimal<N>

source

pub fn zero() -> Decimal<N>

Constructs a decimal number with N / 3 digits of precision representing the number 0.

source

pub fn infinity() -> Decimal<N>

Constructs a decimal number representing positive infinity.

source

pub fn nan() -> Decimal<N>

Constructs a decimal number representing a non-signaling NaN.

source

pub fn digits(&self) -> u32

Computes the number of significant digits in the number.

If the number is zero or infinite, returns 1. If the number is a NaN, returns the number of digits in the payload.

source

pub fn coefficient_digits(&self) -> Vec<u8>

Returns the individual digits of the coefficient in 8-bit, unpacked binary-coded decimal format.

source

pub fn coefficient_units(&self) -> &[u16]

Returns the digits of the coefficient in [decNumberUnit][dnu] format, which is a vector of u16, with element number representing decnumber_sys::DECDPUN digits of the coefficient.

The result is ordered with the least significant digits at index 0.

source

pub fn coefficient<T>(&mut self) -> Result<T, InvalidCoefficientError>
where T: TryFrom<Decimal<N>>,

Returns the value’s coefficient as T or errors if not possible.

All primitive ints are valid for T.

source

pub fn digits_to_lsu_elements_len(digits: u32) -> usize

Returns the number of elements required in the lsu to represent some number of digits.

This function is public and accepts a u32 instead of a Decimal to aid in recomposing (Self::from_raw_parts) values.

source

pub fn exponent(&self) -> i32

Computes the exponent of the number.

source

pub fn set_exponent(&mut self, exponent: i32)

Sets self’s exponent to the provided value.

source

pub fn is_finite(&self) -> bool

Reports whether the number is finite.

A finite number is one that is neither infinite nor a NaN.

source

pub fn is_infinite(&self) -> bool

Reports whether the number is positive or negative infinity.

source

pub fn is_nan(&self) -> bool

Reports whether the number is a NaN.

source

pub fn is_negative(&self) -> bool

Reports whether the number is negative.

A negative number is either negative zero, less than zero, or NaN with a sign of one. This corresponds to Decimal128::is_signed, not Decimal128::is_negative.

source

pub fn is_quiet_nan(&self) -> bool

Reports whether the number is a quiet NaN.

source

pub fn is_signaling_nan(&self) -> bool

Reports whether the number is a signaling NaN.

source

pub fn is_special(&self) -> bool

Reports whether the number has a special value.

A special value is either infinity or NaN. This is the inverse of Decimal::is_finite.

source

pub fn is_zero(&self) -> bool

Reports whether the number is positive or negative zero.

source

pub fn quantum_matches(&self, rhs: &Decimal<N>) -> bool

Reports whether the quantum of the number matches the quantum of rhs.

Quantums are considered to match if the numbers have the same exponent, are both NaNs, or both infinite.

source

pub fn to_decimal32(&self) -> Decimal32

Converts this decimal to a 32-bit decimal float.

The result may be inexact. Use Context::<Decimal32>::from_decimal to observe exceptional conditions.

source

pub fn to_decimal64(&self) -> Decimal64

Converts this decimal to a 64-bit decimal float.

The result may be inexact. Use Context::<Decimal64>::from_decimal to observe exceptional conditions.

source

pub fn to_decimal128(&self) -> Decimal128

Converts this decimal to a 128-bit decimal float.

The result may be inexact. Use Context::<Decimal128>::from_decimal to observe exceptional conditions.

source

pub fn to_raw_parts(&self) -> (u32, i32, u8, [u16; N])

Returns the raw parts of this decimal.

The meaning of these parts are unspecified and subject to change. The only guarantee is that these parts can be supplied as arguments to the Decimal::from_raw_parts to produce a decimal equivalent to the original.

source

pub fn from_raw_parts( digits: u32, exponent: i32, bits: u8, lsu: [u16; N] ) -> Self

Returns a Decimal::<N> with the supplied raw parts, which should be generated using Decimal::to_raw_parts.

source

pub fn to_packed_bcd(&mut self) -> Option<(Vec<u8>, i32)>

Returns self as a Packed Decimal number, including its scale (i.e. its negated exponent) or None for special values.

source

pub fn from_packed_bcd( bcd: &[u8], scale: i32 ) -> Result<Decimal<N>, FromBcdError>

Takes Packed Decimal values and their scales (generated by Self::to_packed_bcd) and returns a Decimal.

§Errors
  • bcd contains more digits than the coefficient permits
  • The adjusted exponent is out of range
  • No sign nibble was found
  • A sign nibble was found before the final nibble
source

pub fn to_standard_notation_string(&self) -> String

Returns a string of the number in standard notation, i.e. guaranteed to not be scientific notation.

source

pub fn trim(&mut self)

Removes insignificant trailing zeros from a number, unconditionally, and stores the modified value in self.

Trait Implementations§

source§

impl<const M: usize, const N: usize> Add<Decimal<M>> for Decimal<N>

source§

fn add(self, rhs: Decimal<M>) -> Self

Note that this clones self to generate the output. For a non-cloning method, use Context::<N>::add.

§

type Output = Decimal<N>

The resulting type after applying the + operator.
source§

impl<const M: usize, const N: usize> AddAssign<Decimal<M>> for Decimal<N>

source§

fn add_assign(&mut self, rhs: Decimal<M>)

Performs the += operation. Read more
source§

impl<const N: usize> Clone for Decimal<N>

source§

fn clone(&self) -> Decimal<N>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const N: usize> Debug for Decimal<N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const N: usize> Default for Decimal<N>

source§

fn default() -> Decimal<N>

Returns the “default value” for a type. Read more
source§

impl<'de, const N: usize> Deserialize<'de> for Decimal<N>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<const N: usize> Display for Decimal<N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const M: usize, const N: usize> Div<Decimal<M>> for Decimal<N>

source§

fn div(self, rhs: Decimal<M>) -> Self

Note that this clones self to generate the output. For a non-cloning method, use Context::<N>::div.

§

type Output = Decimal<N>

The resulting type after applying the / operator.
source§

impl<const M: usize, const N: usize> DivAssign<Decimal<M>> for Decimal<N>

source§

fn div_assign(&mut self, rhs: Decimal<M>)

Performs the /= operation. Read more
source§

impl<const N: usize> From<Decimal128> for Decimal<N>

source§

fn from(n: Decimal128) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<Decimal32> for Decimal<N>

source§

fn from(n: Decimal32) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<Decimal64> for Decimal<N>

source§

fn from(n: Decimal64) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<f32> for Decimal<N>

source§

fn from(n: f32) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<f64> for Decimal<N>

source§

fn from(n: f64) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<i16> for Decimal<N>

source§

fn from(n: i16) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<i32> for Decimal<N>

source§

fn from(n: i32) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<i64> for Decimal<N>

source§

fn from(n: i64) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<i8> for Decimal<N>

source§

fn from(n: i8) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<isize> for Decimal<N>

source§

fn from(n: isize) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<u16> for Decimal<N>

source§

fn from(n: u16) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<u32> for Decimal<N>

source§

fn from(n: u32) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<u64> for Decimal<N>

source§

fn from(n: u64) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<u8> for Decimal<N>

source§

fn from(n: u8) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<usize> for Decimal<N>

source§

fn from(n: usize) -> Decimal<N>

Converts to this type from the input type.
source§

impl<const N: usize> FromStr for Decimal<N>

§

type Err = ParseDecimalError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Decimal<N>, ParseDecimalError>

Parses a string s to return a value of this type. Read more
source§

impl<const M: usize, const N: usize> Mul<Decimal<M>> for Decimal<N>

source§

fn mul(self, rhs: Decimal<M>) -> Self

Note that this clones self to generate the output. For a non-cloning method, use Context::<N>::mul.

§

type Output = Decimal<N>

The resulting type after applying the * operator.
source§

impl<const M: usize, const N: usize> MulAssign<Decimal<M>> for Decimal<N>

source§

fn mul_assign(&mut self, rhs: Decimal<M>)

Performs the *= operation. Read more
source§

impl<const N: usize> Neg for Decimal<N>

source§

fn neg(self) -> Decimal<N>

Note that this clones self to generate the negative value. For a non-cloning method, use Context::<N>::neg.

§

type Output = Decimal<N>

The resulting type after applying the - operator.
source§

impl<const N: usize> PartialEq for Decimal<N>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialOrd for Decimal<N>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, const M: usize, const N: usize> Product<&'a Decimal<M>> for Decimal<N>

source§

fn product<I>(iter: I) -> Self
where I: Iterator<Item = &'a Decimal<M>>,

Method which takes an iterator and generates Self from the elements by multiplying the items.
source§

impl<const M: usize, const N: usize> Product<Decimal<M>> for Decimal<N>

source§

fn product<I>(iter: I) -> Self
where I: Iterator<Item = Decimal<M>>,

Method which takes an iterator and generates Self from the elements by multiplying the items.
source§

impl<const M: usize, const N: usize> Rem<Decimal<M>> for Decimal<N>

source§

fn rem(self, rhs: Decimal<M>) -> Self

Note that this clones self to generate the output. For a non-cloning method, use Context::<N>::rem.

§

type Output = Decimal<N>

The resulting type after applying the % operator.
source§

impl<const M: usize, const N: usize> RemAssign<Decimal<M>> for Decimal<N>

source§

fn rem_assign(&mut self, rhs: Decimal<M>)

Performs the %= operation. Read more
source§

impl<const N: usize> Serialize for Decimal<N>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<const M: usize, const N: usize> Sub<Decimal<M>> for Decimal<N>

source§

fn sub(self, rhs: Decimal<M>) -> Self

Note that this clones self to generate the output. For a non-cloning method, use Context::<N>::sub.

§

type Output = Decimal<N>

The resulting type after applying the - operator.
source§

impl<const M: usize, const N: usize> SubAssign<Decimal<M>> for Decimal<N>

source§

fn sub_assign(&mut self, rhs: Decimal<M>)

Performs the -= operation. Read more
source§

impl<'a, const M: usize, const N: usize> Sum<&'a Decimal<M>> for Decimal<N>

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = &'a Decimal<M>>,

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<const M: usize, const N: usize> Sum<Decimal<M>> for Decimal<N>

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = Decimal<M>>,

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<const N: usize> TryFrom<Decimal<N>> for f32

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<f32, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for f64

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<f64, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for i128

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<i128, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for i16

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<i16, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for i32

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<i32, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for i64

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<i64, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for i8

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<i8, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for isize

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<isize, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for u128

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<u128, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for u16

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<u16, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for u32

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<u32, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for u64

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<u64, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for u8

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<u8, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<Decimal<N>> for usize

Refer to the comments on Context<Decimal<N>>::try_into_i32(), which also apply to this trait.

§

type Error = TryFromDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: Decimal<N>) -> Result<usize, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<i128> for Decimal<N>

Generates a Decimal from an i128 or fails if the result would be imprecise, e.g. has more than N*3 digits of precision.

For an infallible version of this function, see Context<Decimal<N>>::from_i128.

§

type Error = TryIntoDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: i128) -> Result<Decimal<N>, Self::Error>

Performs the conversion.
source§

impl<const N: usize> TryFrom<u128> for Decimal<N>

Generates a Decimal from a u128 or fails if the result would be imprecise, e.g. has more than N*3 digits of precision.

For an infallible version of this function, see Context<Decimal<N>>::from_u128.

§

type Error = TryIntoDecimalError

The type returned in the event of a conversion error.
source§

fn try_from(n: u128) -> Result<Decimal<N>, Self::Error>

Performs the conversion.
source§

impl<const N: usize> Copy for Decimal<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Decimal<N>

§

impl<const N: usize> RefUnwindSafe for Decimal<N>

§

impl<const N: usize> Send for Decimal<N>

§

impl<const N: usize> Sync for Decimal<N>

§

impl<const N: usize> Unpin for Decimal<N>

§

impl<const N: usize> UnwindSafe for Decimal<N>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,