Struct cosmwasm_std::Decimal
source · [−]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
sourceimpl Decimal
impl Decimal
pub const MAX: Self = Self(Uint128::MAX)
sourcepub const fn new(value: Uint128) -> Self
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.
sourcepub const fn raw(value: u128) -> Self
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.
sourcepub fn from_atomics(
atomics: impl Into<Uint128>,
decimal_places: u32
) -> Result<Self, DecimalRangeExceeded>
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");
sourcepub fn from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Self
pub fn from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Self
Returns the ratio (numerator / denominator) as a Decimal
sourcepub fn checked_from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Result<Self, CheckedFromRatioError>
pub fn checked_from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>
) -> Result<Self, 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_mul(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
Multiplies one Decimal
by another, returning an OverflowError
if an overflow occurred.
sourcepub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError>
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.
Trait Implementations
sourceimpl AddAssign<&'_ Decimal> for Decimal
impl AddAssign<&'_ Decimal> for Decimal
sourcefn add_assign(&mut self, other: &Decimal)
fn add_assign(&mut self, other: &Decimal)
Performs the +=
operation. Read more
sourceimpl AddAssign<Decimal> for Decimal
impl AddAssign<Decimal> for Decimal
sourcefn add_assign(&mut self, rhs: Decimal)
fn add_assign(&mut self, rhs: Decimal)
Performs the +=
operation. Read more
sourceimpl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Deserializes as a base64 string
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl DivAssign<&'_ Decimal> for Decimal
impl DivAssign<&'_ Decimal> for Decimal
sourcefn div_assign(&mut self, other: &Decimal)
fn div_assign(&mut self, other: &Decimal)
Performs the /=
operation. Read more
sourceimpl DivAssign<Decimal> for Decimal
impl DivAssign<Decimal> for Decimal
sourcefn div_assign(&mut self, rhs: Decimal)
fn div_assign(&mut self, rhs: Decimal)
Performs the /=
operation. Read more
sourceimpl DivAssign<Uint128> for Decimal
impl DivAssign<Uint128> for Decimal
sourcefn div_assign(&mut self, rhs: Uint128)
fn div_assign(&mut self, rhs: Uint128)
Performs the /=
operation. Read more
sourceimpl FromStr for Decimal
impl FromStr for Decimal
sourceimpl JsonSchema for Decimal
impl JsonSchema for Decimal
sourcefn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
sourcefn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
sourcefn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the $ref
keyword. Read more
sourceimpl 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).
sourceimpl MulAssign<&'_ Decimal> for Decimal
impl MulAssign<&'_ Decimal> for Decimal
sourcefn mul_assign(&mut self, other: &Decimal)
fn mul_assign(&mut self, other: &Decimal)
Performs the *=
operation. Read more
sourceimpl MulAssign<Decimal> for Decimal
impl MulAssign<Decimal> for Decimal
sourcefn mul_assign(&mut self, rhs: Decimal)
fn mul_assign(&mut self, rhs: Decimal)
Performs the *=
operation. Read more
sourceimpl Ord for Decimal
impl Ord for Decimal
sourceimpl PartialOrd<Decimal> for Decimal
impl PartialOrd<Decimal> for Decimal
sourcefn partial_cmp(&self, other: &Decimal) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl SubAssign<&'_ Decimal> for Decimal
impl SubAssign<&'_ Decimal> for Decimal
sourcefn sub_assign(&mut self, other: &Decimal)
fn sub_assign(&mut self, other: &Decimal)
Performs the -=
operation. Read more
sourceimpl SubAssign<Decimal> for Decimal
impl SubAssign<Decimal> for Decimal
sourcefn sub_assign(&mut self, rhs: Decimal)
fn sub_assign(&mut self, rhs: Decimal)
Performs the -=
operation. Read more
impl Copy for Decimal
impl Eq for Decimal
impl StructuralEq for Decimal
impl StructuralPartialEq for Decimal
Auto Trait Implementations
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnwindSafe for Decimal
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more