【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 2 | 型別 Types & 變數 Variables

【Integer】

在 Solidity, uint 就是 uint256,為一個 256-bit 的 unsigned integer。同時也可以宣告其他 bits,或使用 int 來宣告其他的負整數。

uint 在其後宣告完 bits 之後該整數的數值範圍為 0 ~ (2^n) - 1。 int 在其後宣告完 bits 之後該整數的數值範圍為 -2^(n-1) ~ (2^(n-1)) - 1。

pragma solidity ^0.8.11;

contract MyInt {
    uint256 public myUint = 566778778787;
    uint32 public myUint32 = 4294967295;
    uint16 public myUint16 = 65535;
    uint8 public myUint8 = 255;
    
    int256 public myInt = -566778778787;
    int32 public myInt32 = -2147483648;
    int16 public myInt16 = -32768;
    int8 public myInt8 = -128;
}

需要注意的是在 Solidity 中沒有 double 和 float 這些小數型態,也就是說如果我們要使用小數在當前單位,則要作單位換算成更小單位,如此一來就能表現成整數型態了。

Previous| Chapter 2 | 型別 Types & 變數 VariablesNext【Bool】

Last updated 3 years ago

Was this helpful?