pub struct Decimal { /* private fields */ }Expand description
An arbitrary-precision Decimal type with a distinct representation of negative zero (-0).
A Decimal can be thought of as a (coefficient, exponent) pair, and its value can be
calculated using the formula coefficient * 10^exponent.
use ion_rs::{Int, Decimal, UInt};
use ion_rs::decimal::Sign;
// Equivalent to: 1225 * 10^-2, or 12.25
let decimal = Decimal::new(1225, -2);
// The coefficient can be viewed as a sign/magnitude pair...
assert_eq!(decimal.coefficient().sign(), Sign::Positive);
assert_eq!(decimal.coefficient().magnitude(), UInt::from(1225u64));
// ...or, if it is not negative zero, by converting it into an Int.
let coefficient: Int = decimal.coefficient().try_into().expect("`decimal` is not negative zero");
assert_eq!(coefficient, Int::from(1225));
assert_eq!(decimal.exponent(), -2);Implementations§
Source§impl Decimal
impl Decimal
pub const ZERO: Decimal
pub const NEGATIVE_ZERO: Decimal
Sourcepub fn new<C: Into<Coefficient>, E: Into<i64>>(
coefficient: C,
exponent: E,
) -> Decimal
pub fn new<C: Into<Coefficient>, E: Into<i64>>( coefficient: C, exponent: E, ) -> Decimal
Constructs a new Decimal with the provided components. The value of the decimal is:
coefficient * 10^exponent
Sourcepub fn coefficient(&self) -> Coefficient
pub fn coefficient(&self) -> Coefficient
Returns this Decimal’s coefficient.
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§
impl Eq for Decimal
Source§impl Ord for Decimal
impl Ord for Decimal
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Decimal
impl PartialOrd for Decimal
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.
Auto Trait Implementations§
impl Freeze for Decimal
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnsafeUnpin for Decimal
impl UnwindSafe for Decimal
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§impl<V> IntoAnnotatedElement for V
impl<V> IntoAnnotatedElement for V
Source§fn with_annotations<I: IntoAnnotations>(self, annotations: I) -> Element
fn with_annotations<I: IntoAnnotations>(self, annotations: I) -> Element
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more