0

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.

  • 1
    It would be helpful if you copied here the relevant lines of code and the error message. – Ami Heines Jan 7 '19 at 4:48
0

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;

where my_balance is your account wallet (something like this 4.0000 EOS)
now you can use tosend variable for your transaction.

  • 1
    thanks i did find a solution, i was conrrectly multiplying everything by 10000 but my problem, was eosio.token transfer was calling my contract and i had overrided apply so it was calling my another function which was giving weird errrors so added a guard inside that function and now it's ok – rawinput Jan 7 '19 at 11:26
0

An 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.