Struct Decimal

Source
pub struct Decimal<T: Copy + Integer> { /* private fields */ }

Implementations§

Source§

impl<T: Copy + Integer + FromPrimitive + ToPrimitive> Decimal<T>

Source

pub fn from_parts(mantissa: T, exponent: Exp) -> Decimal<T>

Create Decimal from mantissa and exponent.

Source

pub fn round(&self) -> Decimal<T>

Rounds to nearest integer. Half-way cases are rounded away from zero. Doesn’t alter precision.

Source

pub fn to_f64(&self) -> f64

Convert to f64, possibly with precision loss and overflows. Currently conversion isn’t perfect, i. e. it may be inexact even when exact conversion is possible.

Source

pub fn to_f32(&self) -> f32

Convert to f32, possibly with precision loss and overflows. Currently conversion isn’t perfect, i. e. it may be inexact even when exact conversion is possible.

Source

pub fn to_int<U: Clone + Integer + FromPrimitive + From<T>>(&self) -> U

Convert to an integer, truncating fractional part. May overflow.

Source

pub fn to_parts(&self) -> (T, Exp)

Returns significand and exponent.

Source

pub fn canonical(&self) -> Decimal<T>

Source

pub fn int_div(&self, other: Decimal<T>) -> T

Returns result of division, rounded to integer. It’s same as (*this / other).to_int() but is less likely to overflow internally.

Trait Implementations§

Source§

impl<T> Add for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<T> AddAssign for Decimal<T>

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<T: Clone + Copy + Integer> Clone for Decimal<T>

Source§

fn clone(&self) -> Decimal<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Copy + Integer> Debug for Decimal<T>

Source§

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

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

impl<'de, T> Deserialize<'de> for Decimal<T>

Source§

fn deserialize<D>(deserializer: D) -> Result<Decimal<T>, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Display for Decimal<T>

Source§

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

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

impl<T> Div for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Decimal<T>

Performs the / operation. Read more
Source§

impl<U, T> From<U> for Decimal<T>

Source§

fn from(u: U) -> Self

Converts to this type from the input type.
Source§

impl<T> FromStr for Decimal<T>

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

TODO: better validation and proper error handling. Now all valid inputs can be parsed, but some invalid also pass.

Source§

type Err = ParseDecimalError

The associated error which can be returned from parsing.
Source§

impl<T> Mul for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl<T> Neg for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<T> Num for Decimal<T>

Source§

type FromStrRadixErr = DecimalFromStrRadixErr

Source§

fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Convert from a string and radix (typically 2..=36). Read more
Source§

impl<T> One for Decimal<T>

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl<T> Ord for Decimal<T>

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl<T> PartialEq for Decimal<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<T> PartialOrd for Decimal<T>

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 · 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 · 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 · 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 · 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<T> Rem for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Self

Performs the % operation. Read more
Source§

impl<T> Serialize for Decimal<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> Signed for Decimal<T>

Source§

fn abs(&self) -> Self

Computes the absolute value. Read more
Source§

fn abs_sub(&self, other: &Self) -> Self

The positive difference of two numbers. Read more
Source§

fn signum(&self) -> Self

Returns the sign of the number. Read more
Source§

fn is_positive(&self) -> bool

Returns true if the number is positive and false if the number is zero or negative.
Source§

fn is_negative(&self) -> bool

Returns true if the number is negative and false if the number is zero or positive.
Source§

impl<T> Sub for Decimal<T>

Source§

type Output = Decimal<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<T> Zero for Decimal<T>

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<T: Copy + Copy + Integer> Copy for Decimal<T>

Source§

impl<T> Eq for Decimal<T>

Auto Trait Implementations§

§

impl<T> Freeze for Decimal<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Decimal<T>
where T: RefUnwindSafe,

§

impl<T> Send for Decimal<T>
where T: Send,

§

impl<T> Sync for Decimal<T>
where T: Sync,

§

impl<T> Unpin for Decimal<T>
where T: Unpin,

§

impl<T> UnwindSafe for Decimal<T>
where T: UnwindSafe,

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<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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,