How do I compute a percentage of an amount? I tried multiplying by 10000 and after dividing afterward but I get an error when sending that amount.
First of all you need to define your percentage variable as a double. After that you can do like this:
const double amount = 25.0; const double total = 50.0; const double percentage = (amount * 100.0) / total; print("percentage is .....__________________________________", percentage);
you can also recieve these variable as a function parameter.
secondly : if you are using percentage in terms of EOS you should define a varialbe that hold the amount you want to send(after computing your percentage)
eosio::asset tosend = (my_balance * (percentage * 100)) / 10000;
my_balance is your account wallet (something like this 4.0000 EOS)
now you can use
tosend variable for your transaction.
eosio::asset::amount is of type
int64_t, therefore when you divide, any decimal places will be truncated. For example:
int x = 15 / 10; // *should* equal 1.5 eosio::print(x); // Output will be 1, because the .5 is rounded down because x is an integer
Therefore, you must be aware that doing division with
eosio::asset will cause some loss of precision. However, to do division with
eosio::asset, you would do so as follows:
eosio::asset x(15000, eosio::symbol("EOS",4)); // Create 1.5 EOS eosio::asset y(x.amount/10, eosio::symbol("EOS",4)); // Set asset y to have 10th of x