6

I've looked into the following answers, but have not found what I am looking for.

Any help is appreciated.

blockchaindotsol
  • 327
  • 1
  • 3
  • 12

1 Answers1

14

As seen in SO copying and pasting shamelessly to keep the users here.

It's probably best (lowest gas cost and trivial to implement) to perform that calculation on the client rather than in Solidity.

pragma solidity ^0.4.6;

contract Divide {

  function percent(uint numerator, uint denominator, uint precision) public 

  constant returns(uint quotient) {

         // caution, check safe-to-multiply here
        uint _numerator  = numerator * 10 ** (precision+1);
        // with rounding of last digit
        uint _quotient =  ((_numerator / denominator) + 5) / 10;
        return ( _quotient);
  }

}

If you feed it 101,450, 3 you get 224, i.e. 22.4%.

niksmac
  • 9,673
  • 2
  • 40
  • 72