🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

Numbers

In Solidity, there are several types of numbers that can be used as state variables or function parameters. Some of the most common ones are:

  1. uint: an unsigned integer, which can only store non-negative values. It can be of various sizes, such as uint8, uint16, uint32, etc., up to uint256.

  2. int: a signed integer, which can store both positive and negative values. It can also be of various sizes, such as int8, int16, int32, etc., up to int256.

  3. fixed point numbers: numbers with a decimal point, which can have a fixed number of decimal places. They are represented by the types fixed and ufixed, followed by the number of decimal places, such as fixed256x18.

  4. byte: a single byte of data, which can store values between 0 and 255.

  5. bytes: a dynamic array of bytes, which can store any number of bytes.

Here's an example of a Solidity function that uses some of these types:


function calculateAverage(uint[] memory numbers) public returns (fixed256x18) {
    uint sum = 0;
    for (uint i = 0; i < numbers.length; i++) {
        sum += numbers[i];
    }
    return fixed256x18(sum) / fixed256x18(numbers.length);
}

In this example, the function takes an array of unsigned integers as a parameter, and returns a fixed point number (with 18 decimal places) representing the average of the numbers in the array. The function first calculates the sum of the numbers using an unsigned integer variable, and then divides the sum by the length of the array using fixed point division.

PreviousData LocationNextModifiers

Last updated 2 years ago