pub struct Decimal(_);
Expand description

A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0

The greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)

Implementations§

source§

impl Decimal

source

pub const DECIMAL_PLACES: u32 = 18u32

The number of decimal places. Since decimal types are fixed-point rather than floating-point, this is a constant.

source

pub const MAX: Self = _

The largest value that can be represented by this decimal type.

source

pub const MIN: Self = _

The smallest value that can be represented by this decimal type.

source

pub const fn new(value: Uint128) -> Self

Creates a Decimal(value) This is equivalent to Decimal::from_atomics(value, 18) but usable in a const context.

source

pub const fn raw(value: u128) -> Self

Creates a Decimal(Uint128(value)) This is equivalent to Decimal::from_atomics(value, 18) but usable in a const context.

source

pub const fn one() -> Self

Create a 1.0 Decimal

source

pub const fn zero() -> Self

Create a 0.0 Decimal

source

pub fn percent(x: u64) -> Self

Convert x% into Decimal

source

pub fn permille(x: u64) -> Self

Convert permille (x/1000) into Decimal

source

pub fn from_atomics( atomics: impl Into<Uint128>, decimal_places: u32 ) -> Result<Self, DecimalRangeExceeded>

Creates a decimal from a number of atomic units and the number of decimal places. The inputs will be converted internally to form a decimal with 18 decimal places. So the input 123 and 2 will create the decimal 1.23.

Using 18 decimal places is slightly more efficient than other values as no internal conversion is necessary.

Examples
let a = Decimal::from_atomics(Uint128::new(1234), 3).unwrap();
assert_eq!(a.to_string(), "1.234");

let a = Decimal::from_atomics(1234u128, 0).unwrap();
assert_eq!(a.to_string(), "1234");

let a = Decimal::from_atomics(1u64, 18).unwrap();
assert_eq!(a.to_string(), "0.000000000000000001");
source

pub fn from_ratio( numerator: impl Into<Uint128>, denominator: impl Into<Uint128> ) -> Self

Returns the ratio (numerator / denominator) as a Decimal

source

pub fn checked_from_ratio( numerator: impl Into<Uint128>, denominator: impl Into<Uint128> ) -> Result<Self, CheckedFromRatioError>

Returns the ratio (numerator / denominator) as a Decimal

source

pub const fn is_zero(&self) -> bool

source

pub const fn atomics(&self) -> Uint128

A decimal is an integer of atomic units plus a number that specifies the position of the decimal dot. So any decimal can be expressed as two numbers.

Examples
// Value with whole and fractional part
let a = Decimal::from_str("1.234").unwrap();
assert_eq!(a.decimal_places(), 18);
assert_eq!(a.atomics(), Uint128::new(1234000000000000000));

// Smallest possible value
let b = Decimal::from_str("0.000000000000000001").unwrap();
assert_eq!(b.decimal_places(), 18);
assert_eq!(b.atomics(), Uint128::new(1));
source

pub const fn decimal_places(&self) -> u32

The number of decimal places. This is a constant value for now but this could potentially change as the type evolves.

See also Decimal::atomics().

source

pub fn floor(&self) -> Self

Rounds value down after decimal places.

source

pub fn ceil(&self) -> Self

Rounds value up after decimal places. Panics on overflow.

source

pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError>

Rounds value up after decimal places. Returns OverflowError on overflow.

source

pub fn checked_add(self, other: Self) -> Result<Self, OverflowError>

source

pub fn checked_sub(self, other: Self) -> Result<Self, OverflowError>

source

pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>

Multiplies one Decimal by another, returning an OverflowError if an overflow occurred.

source

pub fn pow(self, exp: u32) -> Self

Raises a value to the power of exp, panics if an overflow occurred.

source

pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError>

Raises a value to the power of exp, returning an OverflowError if an overflow occurred.

source

pub fn checked_div(self, other: Self) -> Result<Self, CheckedFromRatioError>

source

pub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError>

source

pub fn sqrt(&self) -> Self

Returns the approximate square root as a Decimal.

This should not overflow or panic.

source

pub const fn abs_diff(self, other: Self) -> Self

source

pub fn saturating_add(self, other: Self) -> Self

source

pub fn saturating_sub(self, other: Self) -> Self

source

pub fn saturating_mul(self, other: Self) -> Self

source

pub fn saturating_pow(self, exp: u32) -> Self

Trait Implementations§

source§

impl Add<&Decimal> for &Decimal

§

type Output = <Decimal as Add<Decimal>>::Output

The resulting type after applying the + operator.
source§

fn add(self, other: &Decimal) -> <Decimal as Add<Decimal>>::Output

Performs the + operation. Read more
source§

impl Add<&Decimal> for Decimal

§

type Output = <Decimal as Add<Decimal>>::Output

The resulting type after applying the + operator.
source§

fn add(self, other: &Decimal) -> <Decimal as Add<Decimal>>::Output

Performs the + operation. Read more
source§

impl<'a> Add<Decimal> for &'a Decimal

§

type Output = <Decimal as Add<Decimal>>::Output

The resulting type after applying the + operator.
source§

fn add(self, other: Decimal) -> <Decimal as Add<Decimal>>::Output

Performs the + operation. Read more
source§

impl Add<Decimal> for Decimal

§

type Output = Decimal

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl AddAssign<&Decimal> for Decimal

source§

fn add_assign(&mut self, other: &Decimal)

Performs the += operation. Read more
source§

impl AddAssign<Decimal> for Decimal

source§

fn add_assign(&mut self, rhs: Decimal)

Performs the += operation. Read more
source§

impl Clone for Decimal

source§

fn clone(&self) -> Decimal

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 Debug for Decimal

source§

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

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

impl Default for Decimal

source§

fn default() -> Decimal

Returns the “default value” for a type. Read more
source§

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

Deserializes as a base64 string

source§

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

Deserialize this value from the given Serde deserializer. 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 Div<&Decimal> for &Decimal

§

type Output = <Decimal as Div<Decimal>>::Output

The resulting type after applying the / operator.
source§

fn div(self, other: &Decimal) -> <Decimal as Div<Decimal>>::Output

Performs the / operation. Read more
source§

impl Div<&Decimal> for Decimal

§

type Output = <Decimal as Div<Decimal>>::Output

The resulting type after applying the / operator.
source§

fn div(self, other: &Decimal) -> <Decimal as Div<Decimal>>::Output

Performs the / operation. Read more
source§

impl<'a> Div<Decimal> for &'a Decimal

§

type Output = <Decimal as Div<Decimal>>::Output

The resulting type after applying the / operator.
source§

fn div(self, other: Decimal) -> <Decimal as Div<Decimal>>::Output

Performs the / operation. Read more
source§

impl Div<Decimal> for Decimal

§

type Output = Decimal

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div<Uint128> for Decimal

§

type Output = Decimal

The resulting type after applying the / operator.
source§

fn div(self, rhs: Uint128) -> Self::Output

Performs the / operation. Read more
source§

impl DivAssign<&Decimal> for Decimal

source§

fn div_assign(&mut self, other: &Decimal)

Performs the /= operation. Read more
source§

impl DivAssign<Decimal> for Decimal

source§

fn div_assign(&mut self, rhs: Decimal)

Performs the /= operation. Read more
source§

impl DivAssign<Uint128> for Decimal

source§

fn div_assign(&mut self, rhs: Uint128)

Performs the /= operation. Read more
source§

impl Fraction<Uint128> for Decimal

source§

fn inv(&self) -> Option<Self>

Returns the multiplicative inverse 1/d for decimal d.

If d is zero, none is returned.

source§

fn numerator(&self) -> Uint128

Returns the numerator p
source§

fn denominator(&self) -> Uint128

Returns the denominator q
source§

impl From<Decimal> for Decimal256

source§

fn from(input: Decimal) -> Self

Converts to this type from the input type.
source§

impl FromStr for Decimal

source§

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

Converts the decimal string to a Decimal Possible inputs: “1.23”, “1”, “000012”, “1.123000000” Disallowed: “”, “.23”

This never performs any kind of rounding. More than DECIMAL_PLACES fractional digits, even zeros, result in an error.

§

type Err = StdError

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

impl JsonSchema for Decimal

source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl Mul<&Decimal> for &Decimal

§

type Output = <Decimal as Mul<Decimal>>::Output

The resulting type after applying the * operator.
source§

fn mul(self, other: &Decimal) -> <Decimal as Mul<Decimal>>::Output

Performs the * operation. Read more
source§

impl Mul<&Decimal> for Decimal

§

type Output = <Decimal as Mul<Decimal>>::Output

The resulting type after applying the * operator.
source§

fn mul(self, other: &Decimal) -> <Decimal as Mul<Decimal>>::Output

Performs the * operation. Read more
source§

impl<'a> Mul<Decimal> for &'a Decimal

§

type Output = <Decimal as Mul<Decimal>>::Output

The resulting type after applying the * operator.
source§

fn mul(self, other: Decimal) -> <Decimal as Mul<Decimal>>::Output

Performs the * operation. Read more
source§

impl Mul<Decimal> for Decimal

§

type Output = Decimal

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul<Decimal> for Uint128

Both du and ud with d: Decimal and u: Uint128 returns an Uint128. There is no specific reason for this decision other than the initial use cases we have. If you need a Decimal result for the same calculation, use Decimal(du) or Decimal(ud).

§

type Output = Uint128

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Decimal) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<Uint128> for Decimal

§

type Output = Uint128

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Uint128) -> Self::Output

Performs the * operation. Read more
source§

impl MulAssign<&Decimal> for Decimal

source§

fn mul_assign(&mut self, other: &Decimal)

Performs the *= operation. Read more
source§

impl MulAssign<Decimal> for Decimal

source§

fn mul_assign(&mut self, rhs: Decimal)

Performs the *= operation. Read more
source§

impl Ord for Decimal

source§

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

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<&Decimal> for Decimal

source§

fn eq(&self, rhs: &&Decimal) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Decimal> for &Decimal

source§

fn eq(&self, rhs: &Decimal) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Decimal> for Decimal

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Decimal> for Decimal

source§

fn partial_cmp(&self, other: &Decimal) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Rem<&Decimal> for &Decimal

§

type Output = <Decimal as Rem<Decimal>>::Output

The resulting type after applying the % operator.
source§

fn rem(self, other: &Decimal) -> <Decimal as Rem<Decimal>>::Output

Performs the % operation. Read more
source§

impl Rem<&Decimal> for Decimal

§

type Output = <Decimal as Rem<Decimal>>::Output

The resulting type after applying the % operator.
source§

fn rem(self, other: &Decimal) -> <Decimal as Rem<Decimal>>::Output

Performs the % operation. Read more
source§

impl<'a> Rem<Decimal> for &'a Decimal

§

type Output = <Decimal as Rem<Decimal>>::Output

The resulting type after applying the % operator.
source§

fn rem(self, other: Decimal) -> <Decimal as Rem<Decimal>>::Output

Performs the % operation. Read more
source§

impl Rem<Decimal> for Decimal

source§

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

Panics

This operation will panic if rhs is zero

§

type Output = Decimal

The resulting type after applying the % operator.
source§

impl RemAssign<&Decimal> for Decimal

source§

fn rem_assign(&mut self, other: &Decimal)

Performs the %= operation. Read more
source§

impl RemAssign<Decimal> for Decimal

source§

fn rem_assign(&mut self, rhs: Decimal)

Performs the %= operation. Read more
source§

impl Serialize for Decimal

Serializes as a decimal string

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 Sub<&Decimal> for &Decimal

§

type Output = <Decimal as Sub<Decimal>>::Output

The resulting type after applying the - operator.
source§

fn sub(self, other: &Decimal) -> <Decimal as Sub<Decimal>>::Output

Performs the - operation. Read more
source§

impl Sub<&Decimal> for Decimal

§

type Output = <Decimal as Sub<Decimal>>::Output

The resulting type after applying the - operator.
source§

fn sub(self, other: &Decimal) -> <Decimal as Sub<Decimal>>::Output

Performs the - operation. Read more
source§

impl<'a> Sub<Decimal> for &'a Decimal

§

type Output = <Decimal as Sub<Decimal>>::Output

The resulting type after applying the - operator.
source§

fn sub(self, other: Decimal) -> <Decimal as Sub<Decimal>>::Output

Performs the - operation. Read more
source§

impl Sub<Decimal> for Decimal

§

type Output = Decimal

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl SubAssign<&Decimal> for Decimal

source§

fn sub_assign(&mut self, other: &Decimal)

Performs the -= operation. Read more
source§

impl SubAssign<Decimal> for Decimal

source§

fn sub_assign(&mut self, rhs: Decimal)

Performs the -= operation. Read more
source§

impl<A> Sum<A> for Decimalwhere Self: Add<A, Output = Self>,

source§

fn sum<I: Iterator<Item = A>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl Copy for Decimal

source§

impl Eq for Decimal

source§

impl StructuralEq for Decimal

source§

impl StructuralPartialEq for Decimal

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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 Twhere T: for<'de> Deserialize<'de>,

§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for Twhere T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,

§

impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for Twhere T: for<'r> GroupOps<&'r Rhs, Output>,

§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for Twhere T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,

§

impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for Twhere T: for<'r> ScalarMul<&'r Rhs, Output>,