Substrate's frame_support
module provides a set of types and methods for performing fixed-point arithmetic operations.
These types, such as PerBill
, PerThousand
, and PerMillion
, allow you to represent fractions and perform
calculations with high precision. In this guide, we'll explore these types and their related methods.
PerThing
Trait:
PerThing
trait is the foundation for the fixed-point types in Substrate.PerBill
, PerThousand
, and PerMillion
.PerBill
Type:
PerBill
represents fractions with a precision of one billionth (10^-9).PerBill
is from 0 to 1 billion (inclusive).PerThousand
Type:
PerThousand
represents fractions with a precision of one thousandth (10^-3).PerThousand
is from 0 to 1,000 (inclusive).PerMillion
Type:
PerMillion
represents fractions with a precision of one millionth (10^-6).PerMillion
is from 0 to 1 million (inclusive).PerBill
, PerThousand
, or PerMillion
using the from_parts
method.let fraction = PerThousand::from_parts(250);
creates a PerThousand
instance representing 25%.mul_floor(value)
: Multiplies the fraction by value and rounds down the result.mul_ceil(value)
: Multiplies the fraction by value and rounds up the result.div_floor(value)
: Divides value by the fraction and rounds down the result.div_ceil(value)
: Divides value by the fraction and rounds up the result.is_zero()
: Checks if the fraction is equal to zero.is_one()
: Checks if the fraction is equal to one.deconstruct()
: Returns the raw value of the fraction as an integer.from_rational(numerator, denominator)
: Creates a fraction from a rational number.from_rational_approximation(numerator, denominator)
: Creates an approximation of a rational number.from_float(float)
: Creates a fraction from a floating-point number.
These types and methods in Substrate's frame_support
module provide a convenient way to work with fractions
and perform precise arithmetic operations. They are particularly useful when dealing with percentages, fees,
or any scenario that requires fixed-point calculations.By leveraging the power of PerBill
, PerThousand
, and related methods, you can write more expressive and precise code
when working with fractions and percentages in your Substrate runtime.
Use one of the methods to refactor the get_amount_out
method.
No files edited in this step.