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
impl Decimal
sourcepub const DECIMAL_PLACES: u32 = 18u32
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.
sourcepub const MAX: Decimal = Self(Uint128::MAX)
pub const MAX: Decimal = Self(Uint128::MAX)
The largest value that can be represented by this decimal type.
sourcepub const MIN: Decimal = Self(Uint128::MIN)
pub const MIN: Decimal = Self(Uint128::MIN)
The smallest value that can be represented by this decimal type.
sourcepub const fn new(value: Uint128) -> Decimal
pub const fn new(value: Uint128) -> Decimal
Creates a Decimal(value)
This is equivalent to Decimal::from_atomics(value, 18)
but usable in a const context.
sourcepub const fn raw(value: u128) -> Decimal
pub const fn raw(value: u128) -> Decimal
Creates a Decimal(Uint128(value))
This is equivalent to Decimal::from_atomics(value, 18)
but usable in a const context.
sourcepub fn from_atomics(
atomics: impl Into<Uint128>,
decimal_places: u32
) -> Result<Decimal, DecimalRangeExceeded>
pub fn from_atomics( atomics: impl Into<Uint128>, decimal_places: u32 ) -> Result<Decimal, 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");
sourcepub fn from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Decimal
pub fn from_ratio( numerator: impl Into<Uint128>, denominator: impl Into<Uint128> ) -> Decimal
Returns the ratio (numerator / denominator) as a Decimal
sourcepub fn checked_from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Result<Decimal, CheckedFromRatioError>
pub fn checked_from_ratio( numerator: impl Into<Uint128>, denominator: impl Into<Uint128> ) -> Result<Decimal, CheckedFromRatioError>
Returns the ratio (numerator / denominator) as a Decimal
pub const fn is_zero(&self) -> bool
sourcepub const fn atomics(&self) -> Uint128
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));
sourcepub const fn decimal_places(&self) -> u32
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()
.
sourcepub fn checked_ceil(&self) -> Result<Decimal, RoundUpOverflowError>
pub fn checked_ceil(&self) -> Result<Decimal, RoundUpOverflowError>
Rounds value up after decimal places. Returns OverflowError on overflow.
pub fn checked_add(self, other: Decimal) -> Result<Decimal, OverflowError>
pub fn checked_sub(self, other: Decimal) -> Result<Decimal, OverflowError>
sourcepub fn checked_mul(self, other: Decimal) -> Result<Decimal, OverflowError>
pub fn checked_mul(self, other: Decimal) -> Result<Decimal, OverflowError>
Multiplies one Decimal
by another, returning an OverflowError
if an overflow occurred.
sourcepub fn pow(self, exp: u32) -> Decimal
pub fn pow(self, exp: u32) -> Decimal
Raises a value to the power of exp
, panics if an overflow occurred.
sourcepub fn checked_pow(self, exp: u32) -> Result<Decimal, OverflowError>
pub fn checked_pow(self, exp: u32) -> Result<Decimal, OverflowError>
Raises a value to the power of exp
, returning an OverflowError
if an overflow occurred.
pub fn checked_div( self, other: Decimal ) -> Result<Decimal, CheckedFromRatioError>
pub fn checked_rem(self, other: Decimal) -> Result<Decimal, DivideByZeroError>
sourcepub fn sqrt(&self) -> Decimal
pub fn sqrt(&self) -> Decimal
Returns the approximate square root as a Decimal.
This should not overflow or panic.
pub const fn abs_diff(self, other: Decimal) -> Decimal
pub fn saturating_add(self, other: Decimal) -> Decimal
pub fn saturating_sub(self, other: Decimal) -> Decimal
pub fn saturating_mul(self, other: Decimal) -> Decimal
pub fn saturating_pow(self, exp: u32) -> Decimal
Trait Implementations§
source§impl AddAssign<&Decimal> for Decimal
impl AddAssign<&Decimal> for Decimal
source§fn add_assign(&mut self, other: &Decimal)
fn add_assign(&mut self, other: &Decimal)
+=
operation. Read moresource§impl AddAssign<Decimal> for Decimal
impl AddAssign<Decimal> for Decimal
source§fn add_assign(&mut self, rhs: Decimal)
fn add_assign(&mut self, rhs: Decimal)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Deserializes as a base64 string
source§fn deserialize<D>(
deserializer: D
) -> Result<Decimal, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<Decimal, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
source§impl DivAssign<&Decimal> for Decimal
impl DivAssign<&Decimal> for Decimal
source§fn div_assign(&mut self, other: &Decimal)
fn div_assign(&mut self, other: &Decimal)
/=
operation. Read moresource§impl DivAssign<Decimal> for Decimal
impl DivAssign<Decimal> for Decimal
source§fn div_assign(&mut self, rhs: Decimal)
fn div_assign(&mut self, rhs: Decimal)
/=
operation. Read moresource§impl DivAssign<Uint128> for Decimal
impl DivAssign<Uint128> for Decimal
source§fn div_assign(&mut self, rhs: Uint128)
fn div_assign(&mut self, rhs: Uint128)
/=
operation. Read moresource§impl FadromaDeserialize for Decimal
impl FadromaDeserialize for Decimal
source§fn from_bytes<'a>(de: &mut Deserializer<'a>) -> Result<Self>
fn from_bytes<'a>(de: &mut Deserializer<'a>) -> Result<Self>
Deserializer
.source§impl FadromaSerialize for Decimal
impl FadromaSerialize for Decimal
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn to_bytes(&self, ser: &mut Serializer) -> Result<()>
fn to_bytes(&self, ser: &mut Serializer) -> Result<()>
Serializer
.source§impl From<Decimal> for Decimal256
impl From<Decimal> for Decimal256
source§fn from(input: Decimal) -> Decimal256
fn from(input: Decimal) -> Decimal256
source§impl FromStr for Decimal
impl FromStr for Decimal
source§fn from_str(input: &str) -> Result<Decimal, <Decimal as FromStr>::Err>
fn from_str(input: &str) -> Result<Decimal, <Decimal as FromStr>::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.
source§impl JsonSchema for Decimal
impl JsonSchema for Decimal
source§fn schema_name() -> String
fn schema_name() -> String
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl Mul<Decimal> for Uint128
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).
source§impl MulAssign<&Decimal> for Decimal
impl MulAssign<&Decimal> for Decimal
source§fn mul_assign(&mut self, other: &Decimal)
fn mul_assign(&mut self, other: &Decimal)
*=
operation. Read moresource§impl MulAssign<Decimal> for Decimal
impl MulAssign<Decimal> for Decimal
source§fn mul_assign(&mut self, rhs: Decimal)
fn mul_assign(&mut self, rhs: Decimal)
*=
operation. Read moresource§impl Ord for Decimal
impl Ord for Decimal
source§impl PartialEq<&Decimal> for Decimal
impl PartialEq<&Decimal> for Decimal
source§impl PartialEq<Decimal> for &Decimal
impl PartialEq<Decimal> 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 RemAssign<&Decimal> for Decimal
impl RemAssign<&Decimal> for Decimal
source§fn rem_assign(&mut self, other: &Decimal)
fn rem_assign(&mut self, other: &Decimal)
%=
operation. Read moresource§impl RemAssign<Decimal> for Decimal
impl RemAssign<Decimal> for Decimal
source§fn rem_assign(&mut self, rhs: Decimal)
fn rem_assign(&mut self, rhs: Decimal)
%=
operation. Read moresource§impl Serialize for Decimal
impl Serialize for Decimal
Serializes as a decimal string
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
source§impl SubAssign<&Decimal> for Decimal
impl SubAssign<&Decimal> for Decimal
source§fn sub_assign(&mut self, other: &Decimal)
fn sub_assign(&mut self, other: &Decimal)
-=
operation. Read moresource§impl SubAssign<Decimal> for Decimal
impl SubAssign<Decimal> for Decimal
source§fn sub_assign(&mut self, rhs: Decimal)
fn sub_assign(&mut self, rhs: Decimal)
-=
operation. Read more