0

My goal is to calculate a 2% interest rate against a loan denominated in DAI.

uint256 loanAmount = 500 * 10 ** 18 // $500 in USD

function calculateInterest( uint256 _loanAmount) public view returns (uint256) {
       return 2 / _loanAmount;
    }

However, when I execute the function, the value i get is 0

Any idea what I'm doing wrong?

1 Answer 1

1

Math in Solidity is done entirely using fixed-point numbers.

You can calculate the interest amount as followed:

uint256 private interestPercentage  = 2;
function calculateInterest( uint256 _loanAmount) public view returns (uint256) {
   return _loanAmount * (100 + interestPercentage) / 100 - _loanAmount;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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