1

If this was a calculation or something i'd do myNumber * 5 and out of the contract (on client side) i'd do myNumber / 100 but since solidity doesn't accept decimals i can't do this.

so my question is how can i transfer 5% of the contract balance? thanks

  • 2
    recipient.transfer(address(this).balance / 20) – goodvibration Jun 27 at 15:56
  • @goodvibration thanks, does this round the decimals? – Adrin Jun 28 at 5:02
  • It truncates (floors) the remainder of division by 20. This is typically negligible, because in the worst case, you'll "lose" 19 weis, which is 19 billionth of a billionth of ether. – goodvibration Jun 28 at 5:02
  • For example, if the contract balance is 20.000000000000000019 ether, then you'll transfer 1 ether instead of 1.00000000000000000095 ether. – goodvibration Jun 28 at 5:06
1

Given address recipient, you can do:

recipient.transfer(address(this).balance / 20);

Note that although this truncates (floors) the remainder of division by 20, it is typically negligible because in the worst case you will "lose" 19 wei, where wei is a billionth of a billionth of ether.

For example, if the contract balance is 20.000000000000000019 ether, then you'll transfer 1 ether instead of 1.00000000000000000095 ether.

| improve this answer | |

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.