pub struct Decimal(/* private fields */);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 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 const fn percent(x: u64) -> Decimal
pub const fn percent(x: u64) -> Decimal
Convert x% into Decimal
§Examples
const HALF: Decimal = Decimal::percent(50);
assert_eq!(HALF, Decimal::from_str("0.5").unwrap());Sourcepub const fn permille(x: u64) -> Decimal
pub const fn permille(x: u64) -> Decimal
Convert permille (x/1000) into Decimal
§Examples
const HALF: Decimal = Decimal::permille(500);
assert_eq!(HALF, Decimal::from_str("0.5").unwrap());Sourcepub const fn bps(x: u64) -> Decimal
pub const fn bps(x: u64) -> Decimal
Convert basis points (x/10000) into Decimal
§Examples
const TWO_BPS: Decimal = Decimal::bps(2);
const HALF: Decimal = Decimal::bps(5000);
assert_eq!(TWO_BPS, Decimal::from_str("0.0002").unwrap());
assert_eq!(HALF, Decimal::from_str("0.5").unwrap());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
Sourcepub fn to_uint_floor(self) -> Uint128
pub fn to_uint_floor(self) -> Uint128
Converts this decimal to an unsigned integer by truncating the fractional part, e.g. 22.5 becomes 22.
§Examples
use core::str::FromStr;
use cosmwasm_std::{Decimal, Uint128};
let d = Decimal::from_str("12.345").unwrap();
assert_eq!(d.to_uint_floor(), Uint128::new(12));
let d = Decimal::from_str("12.999").unwrap();
assert_eq!(d.to_uint_floor(), Uint128::new(12));
let d = Decimal::from_str("75.0").unwrap();
assert_eq!(d.to_uint_floor(), Uint128::new(75));Sourcepub fn to_uint_ceil(self) -> Uint128
pub fn to_uint_ceil(self) -> Uint128
Converts this decimal to an unsigned integer by rounting up to the next integer, e.g. 22.3 becomes 23.
§Examples
use core::str::FromStr;
use cosmwasm_std::{Decimal, Uint128};
let d = Decimal::from_str("12.345").unwrap();
assert_eq!(d.to_uint_ceil(), Uint128::new(13));
let d = Decimal::from_str("12.999").unwrap();
assert_eq!(d.to_uint_ceil(), Uint128::new(13));
let d = Decimal::from_str("75.0").unwrap();
assert_eq!(d.to_uint_ceil(), Uint128::new(75));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 for Decimal
impl AddAssign 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
Deserializes as a base64 string
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<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 DivAssign for Decimal
impl DivAssign for Decimal
Source§fn div_assign(&mut self, rhs: Decimal)
fn div_assign(&mut self, rhs: Decimal)
/= operation. Read moreSource§impl From<Decimal> for Decimal256
impl From<Decimal> for Decimal256
Source§fn from(input: Decimal) -> Decimal256
fn from(input: Decimal) -> Decimal256
Source§impl From<Decimal> for SignedDecimal256
impl From<Decimal> for SignedDecimal256
Source§fn from(value: Decimal) -> SignedDecimal256
fn from(value: Decimal) -> SignedDecimal256
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 schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref keyword. Read moreSource§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 for Decimal
impl MulAssign 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 PartialOrd for Decimal
impl PartialOrd for Decimal
Source§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 for Decimal
impl RemAssign for Decimal
Source§fn rem_assign(&mut self, rhs: Decimal)
fn rem_assign(&mut self, rhs: Decimal)
%= operation. Read moreSource§impl Serialize for Decimal
Serializes as a decimal string
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 for Decimal
impl SubAssign for Decimal
Source§fn sub_assign(&mut self, rhs: Decimal)
fn sub_assign(&mut self, rhs: Decimal)
-= operation. Read moreSource§impl TryFrom<Decimal> for SignedDecimal
impl TryFrom<Decimal> for SignedDecimal
Source§type Error = SignedDecimalRangeExceeded
type Error = SignedDecimalRangeExceeded
Source§fn try_from(
value: Decimal,
) -> Result<SignedDecimal, <SignedDecimal as TryFrom<Decimal>>::Error>
fn try_from( value: Decimal, ) -> Result<SignedDecimal, <SignedDecimal as TryFrom<Decimal>>::Error>
Source§impl TryFrom<Decimal256> for Decimal
impl TryFrom<Decimal256> for Decimal
Source§type Error = DecimalRangeExceeded
type Error = DecimalRangeExceeded
Source§fn try_from(
value: Decimal256,
) -> Result<Decimal, <Decimal as TryFrom<Decimal256>>::Error>
fn try_from( value: Decimal256, ) -> Result<Decimal, <Decimal as TryFrom<Decimal256>>::Error>
Source§impl TryFrom<SignedDecimal> for Decimal
impl TryFrom<SignedDecimal> for Decimal
Source§type Error = DecimalRangeExceeded
type Error = DecimalRangeExceeded
Source§fn try_from(
value: SignedDecimal,
) -> Result<Decimal, <Decimal as TryFrom<SignedDecimal>>::Error>
fn try_from( value: SignedDecimal, ) -> Result<Decimal, <Decimal as TryFrom<SignedDecimal>>::Error>
Source§impl TryFrom<SignedDecimal256> for Decimal
impl TryFrom<SignedDecimal256> for Decimal
Source§type Error = DecimalRangeExceeded
type Error = DecimalRangeExceeded
Source§fn try_from(
value: SignedDecimal256,
) -> Result<Decimal, <Decimal as TryFrom<SignedDecimal256>>::Error>
fn try_from( value: SignedDecimal256, ) -> Result<Decimal, <Decimal as TryFrom<SignedDecimal256>>::Error>
impl Copy for Decimal
impl Eq for Decimal
impl StructuralPartialEq for Decimal
Auto Trait Implementations§
impl Freeze for Decimal
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnwindSafe for Decimal
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more