🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Integers

Integers are whole numbers with no fraction part. Solidity has unsigned integers that hold zero and positives, and signed integers that also hold negatives.

uint256 stores a whole number

A setter stores a whole number in a uint256. The free getter reads the same value back.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract WholeBox {
    uint256 public total;

    function setTotal(uint256 newTotal) public {
        total = newTotal;
    }
}

uint8 stores a small whole number

A uint8 holds a smaller range of whole numbers. It still stores and reads back exact values.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract SmallBox {
    uint8 public level;

    function setLevel(uint8 newLevel) public {
        level = newLevel;
    }
}

int256 stores a negative number

An int256 can hold a value below zero. The setter stores a negative and the getter reads it back.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract SignedBox {
    int256 public temperature;

    function setTemperature(int256 newTemperature) public {
        temperature = newTemperature;
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract NumberBox {
    uint256 public whole;
    uint8 public small;
    int256 public signedNumber;

    function setAll(uint256 newWhole, uint8 newSmall, int256 newSigned) public {
        whole = newWhole;
        small = newSmall;
        signedNumber = newSigned;
    }
}

Key points

  • uint256: the standard unsigned integer, holding whole numbers from zero upward.
  • Unsigned: a number type that never holds a negative value.
  • uint8: a smaller unsigned integer that holds values from 0 to 255.
  • int256: a signed integer that holds negative whole numbers, zero, and positives.
  • Signed: a number type that can hold values below zero.
  • Size: the number after uint or int names how much room the value gets.

In this example, the contract stores one value of each integer kind side by side. The setAll function writes a uint256, a uint8, and an int256 in one call. Each free getter reads its value back exactly, including the negative in the int256.

Note

Solidity integers have no fraction part, unlike numbers in many other languages.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

Which type can store the value -20?