Struct dec::Decimal [−][src]
#[repr(C)]pub struct Decimal<const N: usize> { /* fields omitted */ }
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.
Implementations
Constructs a decimal number with N / 3
digits of precision
representing the number 0.
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.
pub fn coefficient<T>(&mut self) -> Result<T, InvalidCoefficientError> where
T: TryFrom<Decimal<N>>,
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
.
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 decomposing (Self::to_raw_parts
) and recomposing
(Self::from_raw_parts
) values.
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 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 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
.
Safety
The raw parts must be valid according to the guarantees required by the
underlying C library, or undefined behavior can result. The easiest way
to uphold these guarantees is to ensure the raw parts originate from a
call to Decimal::to_raw_parts
.
Panics
If lsu_u8
is not a slice that can be recomposed into a &[16]
with
the number of digits implicitly specified by the digits
parameter,
i.e. essentially ceil(digits / decnumber_sys::DECDPUN)
. You can determine the appropriate
number of elements in lsu_u8
using 2 *
Decimal::digits_to_lsu_elements_len
.
Returns a string of the number in standard notation, i.e. guaranteed to not be scientific notation.
Trait Implementations
Performs the +=
operation. Read more
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Performs the /=
operation. Read more
Performs the conversion.
type Err = ParseDecimalError
type Err = ParseDecimalError
The associated error which can be returned from parsing.
Performs the *=
operation. Read more
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
Performs the %=
operation. Read more
Performs the -=
operation. Read more
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
Refer to the comments on Context<Decimal<N>>::try_into_i32()
, which also apply to
this trait.
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
.
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
.
Auto Trait Implementations
impl<const N: usize> RefUnwindSafe for Decimal<N>
impl<const N: usize> UnwindSafe for Decimal<N>
Blanket Implementations
Mutably borrows from an owned value. Read more