Function cfg_primitives::conversion::fixed_point_to_balance
source · pub fn fixed_point_to_balance<FixedPoint: FixedPointNumber<Inner = IntoBalance>, IntoBalance: BaseArithmetic + Copy>(
fixed_point: FixedPoint,
decimals: usize
) -> Result<IntoBalance, ArithmeticError>
Expand description
Transform a fixed point number to a Balance.
The resulting Balance will be represented with the decimals
given.
i.e:
use cfg_primitives::conversion::fixed_point_to_balance;
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 0), 23);
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 3), 23_123);
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 6), 23_123_456);
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 9), 23_123_456_789);
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 12), 23_123_456_789_000);
assert_ok!(fixed_point_to_balance(FixedU64::from_float(23.1234567890), 15), 23_123_456_789_000_000);
use cfg_primitives::conversion::fixed_point_to_balance;
assert_err!(
// The integer part does not fit in a `u64` (FixedU64::Inner type)
fixed_point_to_balance(FixedU64::from_float(23.42), 18),
ArithmeticError::Overflow
);
Maths:
int = (n / DIV)
frac = n - int * DIV
m = 10 ^ d
result = int * m + frac * m / DIV