So my problem is, that I am trying to calculate a percentage for a share, but ink! doesn't support floating points and I have no clue on how I can get a non-floating value that UI will denominate for me later.
For an example, this code will not compile.
let total_amount: u128 = 250;
let share: u128 = 104;
let percentage = (share as f64 /total_amount as f64) * 100 as f64;
// percentage = 41.6
Is there anyway to get a non-floating value for the percentage so I can denominate the returned number to decimals in UI? Or is there any other solutions to my problem?
Use integer math, but scale your number first:
or, if you want more decimals:
then format the number appropriatly on the frontend, for example if your contract returns per_10000, like in the second example, then (or use some formatting library of choice more likely):
You might want to look into using
checked_mul/checked_divorsaturating_mul/saturing_divin your contract code instead of regular* /to make sure you control how your code handles boundary values.