You cannot use floating point math inside of the runtime.
Floating point numbers can lead to non-determinism in computer systems, and that would be exceptionally bad for blockchains since all of them must come to consensus.
Instead, you must make sure to use fixed-point arithmetic within the entire runtime environment, and Substrate even provides tools to help you with this in the crate sp_arithmetic
.
So to answer these specific scenarios:
- Consider dividing by 2 rather than multiplying by .5.
let new_balance = old_balance / 2;
Use the Percent
type or some of the other per_thing
types which behave like a standard percentage, but use fixed point math under the covers.
Use a fixed point number.
In Substrate, user balances are normally represented with a u128
. However, to express these balances in "human form" a fixed point decimal is used. So while a user's real balance as a u128
would be something like 3_141_592_000_000 UNITS
, we would represent it in the UI by first dividing by 10^12
, and displaying it as 3.141592 TOKENS
. Assuming your fixed point decimal is 12. However different networks may make different choices how many decimal places to use.
This decision is arbitrary.