If you often struggle to remember which uint has what max value in Solidity, as I do, this is a cheatsheet for you (and for me, I'll probably use it all the time).
Gas is expensive on Ethereum so it's best to optimize for it and not to use variables that are larger than what we need. If all we are going to store in a variable are numbers from 1 to max 100, it doesn't make sense to use uint256. It's a waste of gas and money.
I always struggle to remember what's the maximum value for each uint, so I've created a cheatsheet. Now I won't have to worry if my chosen uint is going to be enough or too small.
Below, (almost) all the max values for each uint. I've added the number of digits for each value, so for example, if you know that your max value will have no more than 12 digits, you can safely choose uint40, which has 13 digits.
I didn't include anything between uint128 and uint256. If you're going to use such large numbers, you'll probably use uint256 by default.
A few things to know/remember:
uintincreases in steps of 8, from uint8 to uint256
- For an integer type X, you can use
type(X).maxto access the minimum and maximum value representable by the type
uintis an alias for
Now your uints will always be within range and will never take up more space than needed. Happy savings!