pub struct DynamicDecimal { /* private fields */ }Expand description
Decimal real number with 38 digits of precision and dynamic scale.
This type is primarily meant as a serialized form of Fixed that does not
require parameterization. Any Fixed can be converted into
DynamicDecimal and then converted back into any other Fixed, possibly
with a different precision and scale, without loss of precision (beyond that
inherent in change of scale, if any).
§Representation
The number is represented as an i128 significand and a u8 scale, that
together express the value significand * 10**-scale.
§Invariants
These invariants on significand and scale ensure that the value is in canonical form:
-
If
significand != 0 && scale != 0, thensignificandmust not be a multiple of 10. -
If
significand != 0 && scale == 0, thensignificandmay be a multiple of 10. (This is a “denormalized” representation with fewer digits of precision than other forms.) -
If
significand == 0, thenscalemust be zero.
§Arithmetic
There are various arithmetic traits implemented for DynamicDecimal, but they are intended for limited uses; in particular, these functions will panic for overflows.
Implementations§
Source§impl DynamicDecimal
impl DynamicDecimal
Sourcepub const fn new(sig: i128, exponent: u8) -> Self
pub const fn new(sig: i128, exponent: u8) -> Self
Constructs a new DynamicDecimal with value sig * 10**-exponent.
Sourcepub const fn significand(&self) -> i128
pub const fn significand(&self) -> i128
Returns the canonical significand.
Sourcepub fn trunc(&self) -> Self
pub fn trunc(&self) -> Self
Truncates the value to an integer by discarding digits after decimal point
Sourcepub fn trunc_digits(&self, digits: u8) -> Self
pub fn trunc_digits(&self, digits: u8) -> Self
Returns this value, truncated toward zero at digits after the decimal
point.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
True if value is negative
Trait Implementations§
Source§impl Add for DynamicDecimal
impl Add for DynamicDecimal
Source§impl CheckedDiv for DynamicDecimal
impl CheckedDiv for DynamicDecimal
Source§fn checked_div(&self, other: &DynamicDecimal) -> Option<DynamicDecimal>
fn checked_div(&self, other: &DynamicDecimal) -> Option<DynamicDecimal>
None is returned.Source§impl Clone for DynamicDecimal
impl Clone for DynamicDecimal
impl Copy for DynamicDecimal
Source§impl Debug for DynamicDecimal
impl Debug for DynamicDecimal
Source§impl Default for DynamicDecimal
impl Default for DynamicDecimal
Source§impl Display for DynamicDecimal
impl Display for DynamicDecimal
Source§impl Div for DynamicDecimal
impl Div for DynamicDecimal
Source§type Output = DynamicDecimal
type Output = DynamicDecimal
/ operator.Source§fn div(self, other: DynamicDecimal) -> DynamicDecimal
fn div(self, other: DynamicDecimal) -> DynamicDecimal
/ operation. Read moreimpl Eq for DynamicDecimal
Source§impl From<DynamicDecimal> for i128
impl From<DynamicDecimal> for i128
Source§fn from(value: DynamicDecimal) -> Self
fn from(value: DynamicDecimal) -> Self
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§impl From<DynamicDecimal> for f64
impl From<DynamicDecimal> for f64
Source§fn from(value: DynamicDecimal) -> Self
fn from(value: DynamicDecimal) -> Self
Source§impl From<DynamicDecimal> for f32
impl From<DynamicDecimal> for f32
Source§fn from(value: DynamicDecimal) -> Self
fn from(value: DynamicDecimal) -> Self
Source§impl From<i8> for DynamicDecimal
impl From<i8> for DynamicDecimal
Source§impl From<i16> for DynamicDecimal
impl From<i16> for DynamicDecimal
Source§impl From<i32> for DynamicDecimal
impl From<i32> for DynamicDecimal
Source§impl From<i64> for DynamicDecimal
impl From<i64> for DynamicDecimal
Source§impl From<i128> for DynamicDecimal
impl From<i128> for DynamicDecimal
Source§impl From<isize> for DynamicDecimal
impl From<isize> for DynamicDecimal
Source§impl From<u8> for DynamicDecimal
impl From<u8> for DynamicDecimal
Source§impl From<u16> for DynamicDecimal
impl From<u16> for DynamicDecimal
Source§impl From<u32> for DynamicDecimal
impl From<u32> for DynamicDecimal
Source§impl From<u64> for DynamicDecimal
impl From<u64> for DynamicDecimal
Source§impl From<usize> for DynamicDecimal
impl From<usize> for DynamicDecimal
Source§impl FromStr for DynamicDecimal
impl FromStr for DynamicDecimal
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses s as DynamicDecimal.
This accepts the same forms as f64::from_str, except that it rejects
infinities and NaNs (which Fixed does not support), as well as
out-of-range values. Rounds overprecise values to the nearest
representable value, rounding halfway values to even.
Source§type Err = ParseDecimalError
type Err = ParseDecimalError
Source§impl Hash for DynamicDecimal
impl Hash for DynamicDecimal
Source§impl Mul for DynamicDecimal
impl Mul for DynamicDecimal
Source§impl Neg for DynamicDecimal
impl Neg for DynamicDecimal
Source§impl Ord for DynamicDecimal
impl Ord for DynamicDecimal
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for DynamicDecimal
impl PartialEq for DynamicDecimal
Source§impl PartialOrd for DynamicDecimal
impl PartialOrd for DynamicDecimal
Source§impl Rem for DynamicDecimal
impl Rem for DynamicDecimal
impl StructuralPartialEq for DynamicDecimal
Source§impl Sub for DynamicDecimal
impl Sub for DynamicDecimal
Source§impl<const P: usize, const S: usize> TryFrom<DynamicDecimal> for Fixed<P, S>
impl<const P: usize, const S: usize> TryFrom<DynamicDecimal> for Fixed<P, S>
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Deserializes DynamicDecimal into Fixed. If successful, returns the
original value from before serialization. If S < value.exponent, then
some trailing decimals are lost, by rounding toward zero. Returns an
error` if the value is out of range for this type.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for i64
impl TryFrom<DynamicDecimal> for i64
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for i32
impl TryFrom<DynamicDecimal> for i32
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for i16
impl TryFrom<DynamicDecimal> for i16
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for i8
impl TryFrom<DynamicDecimal> for i8
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for isize
impl TryFrom<DynamicDecimal> for isize
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for u128
impl TryFrom<DynamicDecimal> for u128
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for u64
impl TryFrom<DynamicDecimal> for u64
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for u32
impl TryFrom<DynamicDecimal> for u32
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for u16
impl TryFrom<DynamicDecimal> for u16
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for u8
impl TryFrom<DynamicDecimal> for u8
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.
Source§type Error = OutOfRange
type Error = OutOfRange
Source§impl TryFrom<DynamicDecimal> for usize
impl TryFrom<DynamicDecimal> for usize
Source§fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>
Convert from Fixed to integer, rounding toward zero (the same
semantics as Rust casts from float to integer).
Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.