#[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

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

Constructs a decimal number representing positive infinity.

Constructs a decimal number representing a non-signaling NaN.

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.

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

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.

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

All primitive ints are valid for T.

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.

Computes the exponent of the number.

Sets self’s exponent to the provided value.

Reports whether the number is finite.

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

Reports whether the number is positive or negative infinity.

Reports whether the number is a NaN.

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.

Reports whether the number is a quiet NaN.

Reports whether the number is a signaling NaN.

Reports whether the number has a special value.

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

Reports whether the number is positive or negative zero.

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.

Converts this decimal to a 32-bit decimal float.

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

Converts this decimal to a 64-bit decimal float.

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

Converts this decimal to a 128-bit decimal float.

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

Returns the raw parts of this decimal, with the u16 elements of lsu converted to u8.

The meaning of these parts are unspecified and subject to change.

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

Panics

If lsu_in is not a slice with the number of digits implicitly specified by the digits parameter, i.e. essentially ceil(digits / decnumber_sys::DECDPUN).

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

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

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

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

Trait Implementations

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

The resulting type after applying the + operator.

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

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

The resulting type after applying the / operator.

Performs the /= operation. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

The associated error which can be returned from parsing.

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

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

The resulting type after applying the * operator.

Performs the *= operation. Read more

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

The resulting type after applying the - operator.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

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

The resulting type after applying the % operator.

Performs the %= operation. Read more

Serialize this value into the given Serde serializer. Read more

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

The resulting type after applying the - operator.

Performs the -= operation. Read more

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

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

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

The type returned in the event of a conversion error.

Performs the conversion.

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.

The type returned in the event of a conversion error.

Performs the conversion.

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.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.