#[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>
impl<const N: usize> Decimal<N>
sourcepub fn zero() -> Decimal<N>
pub fn zero() -> Decimal<N>
Constructs a decimal number with N / 3
digits of precision
representing the number 0.
sourcepub fn digits(&self) -> u32
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.
sourcepub fn coefficient_digits(&self) -> Vec<u8>
pub fn coefficient_digits(&self) -> Vec<u8>
Returns the individual digits of the coefficient in 8-bit, unpacked binary-coded decimal format.
sourcepub fn coefficient_units(&self) -> &[u16]
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.
sourcepub fn coefficient<T>(&mut self) -> Result<T, InvalidCoefficientError>
pub fn coefficient<T>(&mut self) -> Result<T, InvalidCoefficientError>
Returns the value’s coefficient as T
or errors if not possible.
All primitive ints are valid for T
.
sourcepub fn digits_to_lsu_elements_len(digits: u32) -> usize
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.
sourcepub fn set_exponent(&mut self, exponent: i32)
pub fn set_exponent(&mut self, exponent: i32)
Sets self
’s exponent to the provided value.
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Reports whether the number is finite.
A finite number is one that is neither infinite nor a NaN.
sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Reports whether the number is positive or negative infinity.
sourcepub fn is_negative(&self) -> bool
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
.
sourcepub fn is_quiet_nan(&self) -> bool
pub fn is_quiet_nan(&self) -> bool
Reports whether the number is a quiet NaN.
sourcepub fn is_signaling_nan(&self) -> bool
pub fn is_signaling_nan(&self) -> bool
Reports whether the number is a signaling NaN.
sourcepub fn is_special(&self) -> bool
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
.
sourcepub fn quantum_matches(&self, rhs: &Decimal<N>) -> bool
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.
sourcepub fn to_decimal32(&self) -> Decimal32
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.
sourcepub fn to_decimal64(&self) -> Decimal64
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.
sourcepub fn to_decimal128(&self) -> Decimal128
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.
sourcepub fn to_raw_parts(&self) -> (u32, i32, u8, [u16; N])
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.
sourcepub fn from_raw_parts(
digits: u32,
exponent: i32,
bits: u8,
lsu: [u16; N],
) -> Self
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
.
sourcepub fn to_packed_bcd(&mut self) -> Option<(Vec<u8>, i32)>
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.
sourcepub fn from_packed_bcd(
bcd: &[u8],
scale: i32,
) -> Result<Decimal<N>, FromBcdError>
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
sourcepub fn to_standard_notation_string(&self) -> String
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.
Trait Implementations§
source§impl<const M: usize, const N: usize> AddAssign<Decimal<M>> for Decimal<N>
impl<const M: usize, const N: usize> AddAssign<Decimal<M>> for Decimal<N>
source§fn add_assign(&mut self, rhs: Decimal<M>)
fn add_assign(&mut self, rhs: Decimal<M>)
+=
operation. Read moresource§impl<'de, const N: usize> Deserialize<'de> for Decimal<N>
impl<'de, const N: usize> Deserialize<'de> for Decimal<N>
source§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>,
source§impl<const M: usize, const N: usize> DivAssign<Decimal<M>> for Decimal<N>
impl<const M: usize, const N: usize> DivAssign<Decimal<M>> for Decimal<N>
source§fn div_assign(&mut self, rhs: Decimal<M>)
fn div_assign(&mut self, rhs: Decimal<M>)
/=
operation. Read moresource§impl<const N: usize> From<Decimal128> for Decimal<N>
impl<const N: usize> From<Decimal128> for Decimal<N>
source§fn from(n: Decimal128) -> Decimal<N>
fn from(n: Decimal128) -> Decimal<N>
source§impl<const M: usize, const N: usize> MulAssign<Decimal<M>> for Decimal<N>
impl<const M: usize, const N: usize> MulAssign<Decimal<M>> for Decimal<N>
source§fn mul_assign(&mut self, rhs: Decimal<M>)
fn mul_assign(&mut self, rhs: Decimal<M>)
*=
operation. Read moresource§impl<const N: usize> PartialOrd for Decimal<N>
impl<const N: usize> PartialOrd for Decimal<N>
source§impl<const M: usize, const N: usize> RemAssign<Decimal<M>> for Decimal<N>
impl<const M: usize, const N: usize> RemAssign<Decimal<M>> for Decimal<N>
source§fn rem_assign(&mut self, rhs: Decimal<M>)
fn rem_assign(&mut self, rhs: Decimal<M>)
%=
operation. Read moresource§impl<const M: usize, const N: usize> SubAssign<Decimal<M>> for Decimal<N>
impl<const M: usize, const N: usize> SubAssign<Decimal<M>> for Decimal<N>
source§fn sub_assign(&mut self, rhs: Decimal<M>)
fn sub_assign(&mut self, rhs: Decimal<M>)
-=
operation. Read moresource§impl<const N: usize> TryFrom<Decimal<N>> for i128
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for i16
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for i32
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for i64
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for i8
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for isize
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for u128
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for u16
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for u32
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for u64
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for u8
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.
source§impl<const N: usize> TryFrom<Decimal<N>> for usize
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.
source§impl<const N: usize> TryFrom<i128> for Decimal<N>
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
.
source§impl<const N: usize> TryFrom<u128> for Decimal<N>
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
.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)