I'm aware that Solidity doesn't support double and floating values.

I'd like to know if there's any way, in Solidity, to get the floor of a division, like: floor(x/2), where x can be any arbitrary "uint" value? so we can get: floor(5/2)=2;


Solidity uses integer division, which is equivalent to floored division.

In solidity, 5/2 == 2

Note: Workaround for integer literals feature of solidity

You can try uint b = uint(5)/2;

Solidity is trying to help you to not make mistakes.

  • thanks for the answer. I tried in Remix; but it's not working!
    – Ay.
    Aug 23 '17 at 13:53
  • What's not working? What result do you get? What code are you using, etc. Aug 23 '17 at 17:11
  • I tried: uint b= 5/2; and also int cc= 5/2;
    – Ay.
    Aug 23 '17 at 17:15
  • Try uint b=uint(5)/2; . Sometimes solidity tries to help you to not make mistakes solidity.readthedocs.io/en/develop/types.html#rational-literals
    – Ismael
    Aug 24 '17 at 3:50
  • Thanks, it works now. It'd be nice if you could add your last comment to the answer.
    – Ay.
    Aug 24 '17 at 9:27

In solidity > 0.4.0 (until current version 0.8.x) the solution is like this:

In case of integer literals, you need to type at least one integer explicitly: uint x = uint(5) / 2; x == 2. Otherwise you will get TypeError.

In case of variables, it's automatic: uint a = 5; uint b = 2; uint c = a / b; c == 2.

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.