pub struct Decimal256(/* private fields */);
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§
Source§impl Decimal256
impl Decimal256
pub const MAX: Self
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
pub fn is_zero(&self) -> bool
Sourcepub fn atomics(&self) -> Uint256
pub 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 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 Decimal256::atomics()
.
Trait Implementations§
Source§impl Add<&Decimal256> for Decimal256
impl Add<&Decimal256> for Decimal256
Source§type Output = Decimal256
type Output = Decimal256
+
operator.Source§fn add(self, other: &Decimal256) -> Self
fn add(self, other: &Decimal256) -> Self
+
operation. Read moreSource§impl Add for Decimal256
impl Add for Decimal256
Source§impl Clone for Decimal256
impl Clone for Decimal256
Source§fn clone(&self) -> Decimal256
fn clone(&self) -> Decimal256
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Decimal256
impl Debug for Decimal256
Source§impl Default for Decimal256
impl Default for Decimal256
Source§fn default() -> Decimal256
fn default() -> Decimal256
Source§impl<'de> Deserialize<'de> for Decimal256
Deserializes as a base64 string
impl<'de> Deserialize<'de> for Decimal256
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 Display for Decimal256
impl Display for Decimal256
Source§impl Div<Uint256> for Decimal256
impl Div<Uint256> for Decimal256
Source§impl DivAssign<Uint256> for Decimal256
impl DivAssign<Uint256> for Decimal256
Source§fn div_assign(&mut self, rhs: Uint256)
fn div_assign(&mut self, rhs: Uint256)
/=
operation. Read moreSource§impl Fraction<Uint256> for Decimal256
impl Fraction<Uint256> for Decimal256
Source§impl FromStr for Decimal256
impl FromStr for Decimal256
Source§impl JsonSchema for Decimal256
impl JsonSchema for Decimal256
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<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).
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).
Source§impl Mul<Uint256> for Decimal256
impl Mul<Uint256> for Decimal256
Source§impl Mul for Decimal256
impl Mul for Decimal256
Source§impl Ord for Decimal256
impl Ord for Decimal256
Source§fn cmp(&self, other: &Decimal256) -> Ordering
fn cmp(&self, other: &Decimal256) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Decimal256
impl PartialEq for Decimal256
Source§impl PartialOrd for Decimal256
impl PartialOrd for Decimal256
Source§impl Serialize for Decimal256
Serializes as a decimal string
impl Serialize for Decimal256
Serializes as a decimal string