pub struct Decimal { /* private fields */ }Expand description
An exact decimal value units / 10^9.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub const fn from_units(units: i128) -> Decimal
pub const fn from_units(units: i128) -> Decimal
The decimal units / 10^9.
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
True when the value is strictly below zero.
Sourcepub fn parse(s: &str) -> Result<Decimal, DecimalError>
pub fn parse(s: &str) -> Result<Decimal, DecimalError>
Parse -?(0|[1-9][0-9]*)(\.[0-9]{1,9})? exactly.
No whitespace, +, exponent, leading zero, bare or trailing . is
accepted. Trailing fractional zeros are accepted (1.50 equals 1.5),
and -0 equals zero. More than nine fractional digits is
DecimalError::TooPrecise; a value outside the i128 unit range is
DecimalError::OutOfRange.
Sourcepub fn to_canonical_string(&self) -> String
pub fn to_canonical_string(&self) -> String
The canonical form: no trailing fractional zeros, no . when
integral, never -0. Decimal::parse inverts it exactly.
Sourcepub fn checked_add(self, rhs: Decimal) -> Result<Decimal, ArithError>
pub fn checked_add(self, rhs: Decimal) -> Result<Decimal, ArithError>
Exact sum, or ArithError::Overflow.
Sourcepub fn checked_sub(self, rhs: Decimal) -> Result<Decimal, ArithError>
pub fn checked_sub(self, rhs: Decimal) -> Result<Decimal, ArithError>
Exact difference, or ArithError::Overflow.
Sourcepub fn checked_mul(
self,
rhs: Decimal,
rounding: Rounding,
) -> Result<Decimal, ArithError>
pub fn checked_mul( self, rhs: Decimal, rounding: Rounding, ) -> Result<Decimal, ArithError>
Product rounded to nine fractional digits.
Declared bound: the exact intermediate units_a * units_b must fit in
i128; otherwise ArithError::Overflow, even if the rounded result
would fit.
Sourcepub fn checked_div(
self,
rhs: Decimal,
rounding: Rounding,
) -> Result<Decimal, ArithError>
pub fn checked_div( self, rhs: Decimal, rounding: Rounding, ) -> Result<Decimal, ArithError>
Quotient rounded to nine fractional digits.
Declared bound: the intermediate units_a * 10^9 must fit in i128;
otherwise ArithError::Overflow. A zero divisor is
ArithError::DivisionByZero.
Sourcepub fn round_to_scale(
self,
scale: u32,
rounding: Rounding,
) -> Result<Decimal, ArithError>
pub fn round_to_scale( self, scale: u32, rounding: Rounding, ) -> Result<Decimal, ArithError>
Round to scale fractional digits (0..=9); a larger scale is
ArithError::InvalidScale. Rounding away from zero at the edge of
the range is ArithError::Overflow.
Sourcepub fn to_f64_nearest(&self) -> f64
pub fn to_f64_nearest(&self) -> f64
The f64 nearest to the exact value (correctly rounded by parsing the
canonical string). Lossy by nature; never used for exact computation.
Trait Implementations§
impl Copy for Decimal
Source§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Source§fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Decimal, D::Error>
fn deserialize<D: Deserializer<'de>>( deserializer: D, ) -> Result<Decimal, D::Error>
Accepts a JSON string only; a JSON number is refused.
impl Eq for Decimal
Source§impl JsonSchema for Decimal
impl JsonSchema for Decimal
Source§fn json_schema(_: &mut SchemaGenerator) -> Schema
fn json_schema(_: &mut SchemaGenerator) -> Schema
A decimal string; the parser alone judges canonical form and bounds.
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read more