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
pub const MAX: Self
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
pub fn is_zero(&self) -> bool
Sourcepub fn atomics(&self) -> Uint128
pub 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 fn decimal_places(&self) -> u32
pub 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()
.
Trait Implementations§
Source§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<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§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 FromStr for Decimal
impl FromStr for Decimal
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 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).
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).