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;
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
Solidity uses integer division, which is equivalent to floored division.
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.
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.