Struct cosmwasm_std::Decimal256
source · [−]pub struct Decimal256(_);
Expand description
A fixed-point decimal value with 18 fractional digits, i.e. Decimal256(1_000_000_000_000_000_000) == 1.0
The greatest possible value that can be represented is 115792089237316195423570985008687907853269984665640564039457.584007913129639935 (which is (2^256 - 1) / 10^18)
Implementations
sourceimpl Decimal256
impl Decimal256
pub const MAX: Self = Self(Uint256::MAX)
sourcepub const fn new(value: Uint256) -> Self
pub const fn new(value: Uint256) -> Self
Creates a Decimal256 from Uint256
This is equivalent to Decimal256::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 Decimal256 from u128
This is equivalent to Decimal256::from_atomics(value, 18)
but usable in a const context.
sourcepub fn from_atomics(
atomics: impl Into<Uint256>,
decimal_places: u32
) -> Result<Self, Decimal256RangeExceeded>
pub fn from_atomics(
atomics: impl Into<Uint256>,
decimal_places: u32
) -> Result<Self, Decimal256RangeExceeded>
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 = Decimal256::from_atomics(1234u64, 3).unwrap();
assert_eq!(a.to_string(), "1.234");
let a = Decimal256::from_atomics(1234u128, 0).unwrap();
assert_eq!(a.to_string(), "1234");
let a = Decimal256::from_atomics(1u64, 18).unwrap();
assert_eq!(a.to_string(), "0.000000000000000001");
let a = Decimal256::from_atomics(Uint256::MAX, 18).unwrap();
assert_eq!(a, Decimal256::MAX);
sourcepub fn from_ratio(
numerator: impl Into<Uint256>,
denominator: impl Into<Uint256>
) -> Self
pub fn from_ratio(
numerator: impl Into<Uint256>,
denominator: impl Into<Uint256>
) -> Self
Returns the ratio (numerator / denominator) as a Decimal256
sourcepub fn checked_from_ratio(
numerator: impl Into<Uint256>,
denominator: impl Into<Uint256>
) -> Result<Self, CheckedFromRatioError>
pub fn checked_from_ratio(
numerator: impl Into<Uint256>,
denominator: impl Into<Uint256>
) -> Result<Self, CheckedFromRatioError>
Returns the ratio (numerator / denominator) as a Decimal256
pub const fn is_zero(&self) -> bool
sourcepub const fn atomics(&self) -> Uint256
pub const fn atomics(&self) -> Uint256
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 = Decimal256::from_str("1.234").unwrap();
assert_eq!(a.decimal_places(), 18);
assert_eq!(a.atomics(), Uint256::from(1234000000000000000u128));
// Smallest possible value
let b = Decimal256::from_str("0.000000000000000001").unwrap();
assert_eq!(b.decimal_places(), 18);
assert_eq!(b.atomics(), Uint256::from(1u128));
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 Decimal256::atomics()
.
sourcepub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
Multiplies one Decimal256
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 Add<&'_ Decimal256> for Decimal256
impl Add<&'_ Decimal256> for Decimal256
type Output = <Decimal256 as Add<Decimal256>>::Output
type Output = <Decimal256 as Add<Decimal256>>::Output
The resulting type after applying the +
operator.
sourcefn add(self, other: &Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
fn add(self, other: &Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
Performs the +
operation. Read more
sourceimpl Add<&'_ Decimal256> for &Decimal256
impl Add<&'_ Decimal256> for &Decimal256
type Output = <Decimal256 as Add<Decimal256>>::Output
type Output = <Decimal256 as Add<Decimal256>>::Output
The resulting type after applying the +
operator.
sourcefn add(self, other: &Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
fn add(self, other: &Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
Performs the +
operation. Read more
sourceimpl Add<Decimal256> for Decimal256
impl Add<Decimal256> for Decimal256
type Output = Decimal256
type Output = Decimal256
The resulting type after applying the +
operator.
sourceimpl<'a> Add<Decimal256> for &'a Decimal256
impl<'a> Add<Decimal256> for &'a Decimal256
type Output = <Decimal256 as Add<Decimal256>>::Output
type Output = <Decimal256 as Add<Decimal256>>::Output
The resulting type after applying the +
operator.
sourcefn add(self, other: Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
fn add(self, other: Decimal256) -> <Decimal256 as Add<Decimal256>>::Output
Performs the +
operation. Read more
sourceimpl AddAssign<&'_ Decimal256> for Decimal256
impl AddAssign<&'_ Decimal256> for Decimal256
sourcefn add_assign(&mut self, other: &Decimal256)
fn add_assign(&mut self, other: &Decimal256)
Performs the +=
operation. Read more
sourceimpl AddAssign<Decimal256> for Decimal256
impl AddAssign<Decimal256> for Decimal256
sourcefn add_assign(&mut self, rhs: Decimal256)
fn add_assign(&mut self, rhs: Decimal256)
Performs the +=
operation. Read more
sourceimpl Clone for Decimal256
impl Clone for Decimal256
sourcefn clone(&self) -> Decimal256
fn clone(&self) -> Decimal256
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Decimal256
impl Debug for Decimal256
sourceimpl Default for Decimal256
impl Default for Decimal256
sourcefn default() -> Decimal256
fn default() -> Decimal256
Returns the “default value” for a type. Read more
sourceimpl<'de> Deserialize<'de> for Decimal256
impl<'de> Deserialize<'de> for Decimal256
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 Display for Decimal256
impl Display for Decimal256
sourceimpl Div<&'_ Decimal256> for Decimal256
impl Div<&'_ Decimal256> for Decimal256
type Output = <Decimal256 as Div<Decimal256>>::Output
type Output = <Decimal256 as Div<Decimal256>>::Output
The resulting type after applying the /
operator.
sourcefn div(self, other: &Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
fn div(self, other: &Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
Performs the /
operation. Read more
sourceimpl Div<&'_ Decimal256> for &Decimal256
impl Div<&'_ Decimal256> for &Decimal256
type Output = <Decimal256 as Div<Decimal256>>::Output
type Output = <Decimal256 as Div<Decimal256>>::Output
The resulting type after applying the /
operator.
sourcefn div(self, other: &Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
fn div(self, other: &Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
Performs the /
operation. Read more
sourceimpl Div<Decimal256> for Decimal256
impl Div<Decimal256> for Decimal256
type Output = Decimal256
type Output = Decimal256
The resulting type after applying the /
operator.
sourceimpl<'a> Div<Decimal256> for &'a Decimal256
impl<'a> Div<Decimal256> for &'a Decimal256
type Output = <Decimal256 as Div<Decimal256>>::Output
type Output = <Decimal256 as Div<Decimal256>>::Output
The resulting type after applying the /
operator.
sourcefn div(self, other: Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
fn div(self, other: Decimal256) -> <Decimal256 as Div<Decimal256>>::Output
Performs the /
operation. Read more
sourceimpl Div<Uint256> for Decimal256
impl Div<Uint256> for Decimal256
sourceimpl DivAssign<&'_ Decimal256> for Decimal256
impl DivAssign<&'_ Decimal256> for Decimal256
sourcefn div_assign(&mut self, other: &Decimal256)
fn div_assign(&mut self, other: &Decimal256)
Performs the /=
operation. Read more
sourceimpl DivAssign<Decimal256> for Decimal256
impl DivAssign<Decimal256> for Decimal256
sourcefn div_assign(&mut self, rhs: Decimal256)
fn div_assign(&mut self, rhs: Decimal256)
Performs the /=
operation. Read more
sourceimpl DivAssign<Uint256> for Decimal256
impl DivAssign<Uint256> for Decimal256
sourcefn div_assign(&mut self, rhs: Uint256)
fn div_assign(&mut self, rhs: Uint256)
Performs the /=
operation. Read more
sourceimpl Fraction<Uint256> for Decimal256
impl Fraction<Uint256> for Decimal256
sourceimpl FromStr for Decimal256
impl FromStr for Decimal256
sourceimpl JsonSchema for Decimal256
impl JsonSchema for Decimal256
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<&'_ Decimal256> for Decimal256
impl Mul<&'_ Decimal256> for Decimal256
type Output = <Decimal256 as Mul<Decimal256>>::Output
type Output = <Decimal256 as Mul<Decimal256>>::Output
The resulting type after applying the *
operator.
sourcefn mul(self, other: &Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
fn mul(self, other: &Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
Performs the *
operation. Read more
sourceimpl Mul<&'_ Decimal256> for &Decimal256
impl Mul<&'_ Decimal256> for &Decimal256
type Output = <Decimal256 as Mul<Decimal256>>::Output
type Output = <Decimal256 as Mul<Decimal256>>::Output
The resulting type after applying the *
operator.
sourcefn mul(self, other: &Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
fn mul(self, other: &Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
Performs the *
operation. Read more
sourceimpl Mul<Decimal256> for Decimal256
impl Mul<Decimal256> for Decimal256
type Output = Decimal256
type Output = Decimal256
The resulting type after applying the *
operator.
sourceimpl<'a> Mul<Decimal256> for &'a Decimal256
impl<'a> Mul<Decimal256> for &'a Decimal256
type Output = <Decimal256 as Mul<Decimal256>>::Output
type Output = <Decimal256 as Mul<Decimal256>>::Output
The resulting type after applying the *
operator.
sourcefn mul(self, other: Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
fn mul(self, other: Decimal256) -> <Decimal256 as Mul<Decimal256>>::Output
Performs the *
operation. Read more
sourceimpl Mul<Decimal256> for Uint256
impl Mul<Decimal256> for Uint256
Both du and ud with d: Decimal256 and u: Uint256 returns an Uint256. There is no specific reason for this decision other than the initial use cases we have. If you need a Decimal256 result for the same calculation, use Decimal256(du) or Decimal256(ud).
sourceimpl Mul<Uint256> for Decimal256
impl Mul<Uint256> for Decimal256
sourceimpl MulAssign<&'_ Decimal256> for Decimal256
impl MulAssign<&'_ Decimal256> for Decimal256
sourcefn mul_assign(&mut self, other: &Decimal256)
fn mul_assign(&mut self, other: &Decimal256)
Performs the *=
operation. Read more
sourceimpl MulAssign<Decimal256> for Decimal256
impl MulAssign<Decimal256> for Decimal256
sourcefn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the *=
operation. Read more
sourceimpl Ord for Decimal256
impl Ord for Decimal256
sourceimpl PartialEq<Decimal256> for Decimal256
impl PartialEq<Decimal256> for Decimal256
sourcefn eq(&self, other: &Decimal256) -> bool
fn eq(&self, other: &Decimal256) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &Decimal256) -> bool
fn ne(&self, other: &Decimal256) -> bool
This method tests for !=
.
sourceimpl PartialOrd<Decimal256> for Decimal256
impl PartialOrd<Decimal256> for Decimal256
sourcefn partial_cmp(&self, other: &Decimal256) -> Option<Ordering>
fn partial_cmp(&self, other: &Decimal256) -> 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 Serialize for Decimal256
impl Serialize for Decimal256
Serializes as a decimal string
sourceimpl Sub<&'_ Decimal256> for Decimal256
impl Sub<&'_ Decimal256> for Decimal256
type Output = <Decimal256 as Sub<Decimal256>>::Output
type Output = <Decimal256 as Sub<Decimal256>>::Output
The resulting type after applying the -
operator.
sourcefn sub(self, other: &Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
fn sub(self, other: &Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
Performs the -
operation. Read more
sourceimpl Sub<&'_ Decimal256> for &Decimal256
impl Sub<&'_ Decimal256> for &Decimal256
type Output = <Decimal256 as Sub<Decimal256>>::Output
type Output = <Decimal256 as Sub<Decimal256>>::Output
The resulting type after applying the -
operator.
sourcefn sub(self, other: &Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
fn sub(self, other: &Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
Performs the -
operation. Read more
sourceimpl Sub<Decimal256> for Decimal256
impl Sub<Decimal256> for Decimal256
type Output = Decimal256
type Output = Decimal256
The resulting type after applying the -
operator.
sourceimpl<'a> Sub<Decimal256> for &'a Decimal256
impl<'a> Sub<Decimal256> for &'a Decimal256
type Output = <Decimal256 as Sub<Decimal256>>::Output
type Output = <Decimal256 as Sub<Decimal256>>::Output
The resulting type after applying the -
operator.
sourcefn sub(self, other: Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
fn sub(self, other: Decimal256) -> <Decimal256 as Sub<Decimal256>>::Output
Performs the -
operation. Read more
sourceimpl SubAssign<&'_ Decimal256> for Decimal256
impl SubAssign<&'_ Decimal256> for Decimal256
sourcefn sub_assign(&mut self, other: &Decimal256)
fn sub_assign(&mut self, other: &Decimal256)
Performs the -=
operation. Read more
sourceimpl SubAssign<Decimal256> for Decimal256
impl SubAssign<Decimal256> for Decimal256
sourcefn sub_assign(&mut self, rhs: Decimal256)
fn sub_assign(&mut self, rhs: Decimal256)
Performs the -=
operation. Read more
sourceimpl<A> Sum<A> for Decimal256 where
Self: Add<A, Output = Self>,
impl<A> Sum<A> for Decimal256 where
Self: Add<A, Output = Self>,
impl Copy for Decimal256
impl Eq for Decimal256
impl StructuralEq for Decimal256
impl StructuralPartialEq for Decimal256
Auto Trait Implementations
impl RefUnwindSafe for Decimal256
impl Send for Decimal256
impl Sync for Decimal256
impl Unpin for Decimal256
impl UnwindSafe for Decimal256
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