數字類輸入 或 double 代表一個 雙精度 (64 位) 浮點數字。數字範圍從 -1.7 * 10 308 到 1.7 * 10 308 (約有 15 位精度,正或負)。
已簽名和未簽名
數字的符號表示它是否為正或負。例如, 1 是正的,而 -1 是負的。在 Luau 中,數字 -0 相當於 0 。
print(0 == -0) --> 真print(-0 > 1) --> 否print(-0 < 1) --> 真print(-0 > -1) --> 真print(-0 < -1) --> false
數字分類
Luau 不區分整數和數字,但 API 參考有時會區分它們,以便更具體地說明如何使用每個 API。
浮點數
float 數字類型指的是帶有十進位位置的實數。在電腦科學條款中,它們是單精度(32位)漂浮點數,與雙精度漂浮點數相比不那麼精確,但對大多數使用案例來說精確度已足夠,並且需要較少的記憶和儲存空間。
int
integer或int,指的是32位整數,範圍為-231到231-1。預期整數的屬性和功能,當你將非整數傳給它們時,可能會自動圓滿或提升錯誤,當你分配或傳送非整數給它們時。
int64
int64引用了簽名的64位整數,範圍為-263到263-1。這種整數類型常見於使用 Roblox 網站的方法中的 ID 號碼。例如, Player.UserId 是 int64 ,而 MarketplaceService:PromptPurchase() 和 TeleportService:Teleport() 各自期望 int64 對於ID參數。
符號
數字以最顯著的數字首先標示(大端)。在 Luau 中標示數字字面有多種方法:
操作
您可以使用邏輯和關聯 運算符 來操作和比較數字。您也可以在 圖書館和 圖書館中使用數學函數,例如 和 在 圖書館中進行位元操作。
類型介紹
您可以使用 type(x) 或 typeof(x) 來確定值 x 是否是數字。兩者都會返回字串 number 如果 x 是數字。
local testInt = 5local testDecimal = 9.12761656local testString = "Hello"print(type(testInt)) --> 數字print(type(testDecimal)) --> 數字print(type(testString)) --> 字串print(typeof(testInt)) --> 數字print(typeof(testDecimal)) --> 數字print(typeof(testString)) --> string
回合功能
您可以使用 math.floor()、math.ceil() 或 math.modf() 來回合數字。這些功能會返回整數結果,如果 Luau 可以將其視為整數。如果數字過大,Luau將它返回為浮點數。
- 要確定數字 x 是否是整數,請使用 math.floor(x) == x 。
- 要將數字向下回合,請使用 math.floor() 。
- 要將數字向上回合,請使用 math.ceil() 。
- 要將數字向零回合,請使用 math.modf() 。它還會返回回合數的小數差作為第二個結果。
print(math.floor(3.3)) --> 3print(math.floor(-3.3)) --> -4print(math.ceil(3.3)) --> 4print(math.ceil(-3.3)) --> -3print(math.modf(3.3)) --> 3 0.2999999999999998print(math.modf(-3.3)) --> -3 -0.2999999999999998