Max values for each uint in Solidity, from uint8 to uint256

STREAM by Darren Almond

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:

- uint increases in steps of 8, from uint8 to uint256
- For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type
- uint is an alias for uint256

Now your uints will always be within range and will never take up more space than needed. Happy savings!

↑ Like what I'm writing about? Follow me on Twitter.

↓ or get updates when I publish a new article, review a new book or share a new #TIL.

More good stuff