pub struct Decimal { /* private fields */ }
Expand description
An arbitrary-precision Decimal type with a distinct representation of negative zero (-0
).
Implementations§
source§impl Decimal
impl Decimal
sourcepub fn new<I: Into<Coefficient>>(coefficient: I, exponent: i64) -> Decimal
pub fn new<I: Into<Coefficient>>(coefficient: I, exponent: i64) -> Decimal
Constructs a new Decimal with the provided components. The value of the decimal is (coefficient * 10^exponent) * (if sign == Sign::Negative { -1 } else { 1 })
sourcepub fn scale(&self) -> i64
pub fn scale(&self) -> i64
Returns scale of the Decimal value If zero or positive, a scale indicates the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.
sourcepub fn precision(&self) -> u64
pub fn precision(&self) -> u64
Returns the number of digits in the non-scaled integer representation of the decimal.
sourcepub fn negative_zero() -> Decimal
pub fn negative_zero() -> Decimal
Constructs a Decimal with the value -0d0
. This is provided as a convenience method
because Rust will ignore a unary minus when it is applied to an zero literal (-0
).
sourcepub fn negative_zero_with_exponent(exponent: i64) -> Decimal
pub fn negative_zero_with_exponent(exponent: i64) -> Decimal
Constructs a Decimal with a coefficient of -0
and the specified exponent. This function
is provided as a convenience method because Rust will ignore a unary minus when it is
applied to a zero literal (-0
).
sourcepub fn is_less_than_zero(&self) -> bool
pub fn is_less_than_zero(&self) -> bool
Returns true if this Decimal’s coefficient has a negative sign AND a magnitude greater than zero. Otherwise, returns false. (Negative zero returns false.)
Trait Implementations§
source§impl From<BigDecimal> for Decimal
impl From<BigDecimal> for Decimal
Make a Decimal from a BigDecimal. This is a lossless operation.
source§fn from(value: BigDecimal) -> Self
fn from(value: BigDecimal) -> Self
source§impl Ord for Decimal
impl Ord for Decimal
source§impl PartialEq<Decimal> for Decimal
impl PartialEq<Decimal> for Decimal
source§impl PartialOrd<Decimal> for Decimal
impl PartialOrd<Decimal> for Decimal
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl TryFrom<Decimal> for BigDecimal
impl TryFrom<Decimal> for BigDecimal
source§impl TryFrom<f64> for Decimal
impl TryFrom<f64> for Decimal
source§fn try_from(value: f64) -> Result<Self, Self::Error>
fn try_from(value: f64) -> Result<Self, Self::Error>
Attempts to create a Decimal from an f64. Returns an Error if the f64 being converted is a special value, including:
- Infinity
- Negative infinity
- NaN (not-a-number) Otherwise, returns Ok.
Because Decimal can represent negative zero, f64::neg_zero() IS supported.
NOTE: While the resulting decimal will be a very close approximation of the original f64’s value, this is an inherently lossy operation. Floating point values do not encode a precision. When converting an f64 to a Decimal, a precision for the new Decimal must be chosen somewhat arbitrarily. Do NOT rely on the precision of the resulting Decimal. This implementation may change without notice.