Skip to main content

Decimal

Struct Decimal 

Source
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

Source

pub const ZERO: Decimal

Source

pub const NEGATIVE_ZERO: Decimal

Source

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

Source

pub fn coefficient(&self) -> Coefficient

Returns this Decimal’s coefficient.

Source

pub fn exponent(&self) -> i64

Returns this Decimal’s exponent.

Source

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.

Source

pub fn precision(&self) -> u64

Returns the number of digits in the non-scaled integer representation of the decimal.

Source

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).

Source

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).

Source

pub fn is_zero(&self) -> bool

Returns true if this Decimal is a zero of any sign or exponent.

Source

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.)

Source

pub fn trunc(&self) -> Decimal

Returns the integer part of self. This means that non-integer numbers are always truncated towards zero, maintaining the sign of the original coefficient.

Source

pub fn fract(&self) -> Decimal

Returns the fractional part of self. Values with no fractional component will return zero maintaining the sign of the original coefficient.

Trait Implementations§

Source§

impl Clone for Decimal

Source§

fn clone(&self) -> Decimal

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Decimal

Source§

impl From<Decimal> for Value

Source§

fn from(decimal_val: Decimal) -> Self

Converts to this type from the input type.
Source§

impl From<Int> for Decimal

Source§

fn from(value: Int) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Decimal

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Decimal

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Decimal

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Decimal

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Decimal

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Decimal

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Decimal

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Decimal

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Decimal

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Decimal

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl Ord for Decimal

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Decimal

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Decimal

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<Element> for Decimal

Source§

type Error = ConversionOperationError<Element, Decimal>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Decimal>

Performs the conversion.
Source§

impl TryFrom<f32> for Decimal

Source§

type Error = IonError

The type returned in the event of a conversion error.
Source§

fn try_from(value: f32) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<f64> for Decimal

Source§

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.

Source§

type Error = IonError

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<V> IntoAnnotatedElement for V
where V: Into<Value>,

Source§

fn with_annotations<I: IntoAnnotations>(self, annotations: I) -> Element

Converts the value into an Element with the specified annotations.
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.