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

Creates a Decimal256 from Uint256 This is equivalent to Decimal256::from_atomics(value, 18) but usable in a const context.

Creates a Decimal256 from u128 This is equivalent to Decimal256::from_atomics(value, 18) but usable in a const context.

Create a 1.0 Decimal256

Create a 0.0 Decimal256

Convert x% into Decimal256

Convert permille (x/1000) into Decimal256

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);

Returns the ratio (numerator / denominator) as a Decimal256

Returns the ratio (numerator / denominator) as a Decimal256

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));

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().

Multiplies one Decimal256 by another, returning an OverflowError if an overflow occurred.

Raises a value to the power of exp, returning an OverflowError if an overflow occurred.

Returns the approximate square root as a Decimal256.

This should not overflow or panic.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserializes as a base64 string

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Returns the multiplicative inverse 1/d for decimal d.

If d is zero, none is returned.

Returns the numerator p

Returns the denominator q

Converts the decimal string to a Decimal256 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.

The associated error which can be returned from parsing.

The name of the generated JSON Schema. Read more

Generates a JSON Schema for this type. Read more

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

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).

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serializes as a decimal string

Serialize this value into the given Serde serializer. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.