Numbers

The number data type, or double, represents double-precision (64-bit) floating-point numbers. Numbers can range from -1.7 × 10308 to 1.7 × 10308 (around 15 digits of precision, positive or negative).

Signed and Unsigned

The sign of the number indicates whether it's positive or negative. For example, 1 is positive and -1 is negative. In Luau, the number -0 is equivalent to 0.


1print(0 == -0) -- true
2print(-0 > 1) -- false
3print(-0 < 1) -- true
4print(-0 > -1) -- true
5print(-0 < -1) -- false
6

Number Classifications

Luau doesn't distinguish between integers and numbers, but the API Reference Manual sometimes distinguishes between them to be more specific about how to use each API.

float

The float number type refers to a single-precision (32-bit) floating-point number, which range from -231 to 231 - 1. This type isn't as precise as double-precision floating-point numbers, but is sufficiently precise for most use cases and requires less space to store.

int

The integer number type, or int, refers to a 32-bit whole number, which ranges from from -231 to 231 - 1. Properties and functions that expect integers may automatically round or raise errors when you assign or pass non-integers to them.

int64

The int64 number type refers to a signed 64-bit integer, which ranges from -263 to 263 - 1. This type of integer is common for methods that use ID numbers from the Roblox website. For example, Player.UserId is an int64, and MarketplaceService:PromptPurchase() and TeleportService:Teleport() each expect int64 for the ID arguments.

Notation

Numbers are notated with the most significant digits first (big-endian). There are multiple ways to notate number literals in Roblox Lua:

  • Decimal (base-10) — Write the digits of the number normally using digits 0–9 with a single optional decimal point, for example 7, 1.25, or -22.5.
  • Scientific notation — Write a decimal number followed by e or e+, then an integer to raise the decimal number to a power of 10. For instance, 12e3 is 12 × 10^3 (12,000).
  • Hexadecimal (base-16) — Begin the number with 0x followed by digits 0–9 or A–F (capitalization ignored). For example, 0xF is 15 and 0x3FC is 1020.
  • Binary (base-2) — Begin the number with 0b followed by 0s or 1s, for instance 0b1100 (12 in decimal format).

Using Numbers

Operations

You can use Luau math and relational operators to manipulate and compare numbers. You can also use mathematical functions such as math.sqrt() and math.exp() in the math library and bitwise operations in the bit32 library.

Type Introspection

You can determine if a value x is a number by using type(x) or typeof(x). Both return the string number if x is a number.


1local testInt = 5
2local testDecimal = 9.12761656
3local testString = "Hello"
4
5print(type(testInt)) -- number
6print(type(testDecimal)) -- number
7print(type(testString)) -- string
8
9print(typeof(testInt)) -- number
10print(typeof(testDecimal)) -- number
11print(typeof(testString)) -- string
12

Math Library

Rounding Functions

You can round numbers using math.floor(), math.ceil(), or math.modf(). These functions return an integer result if Luau can represent it as an integer. If the number is too large, Luau returns it as a float.

  • To determine if a number x is an integer, use math.floor(x) == x.
  • To round a number down, use math.floor().
  • To round a number up, use math.ceil().
  • To round a number towards zero, use math.modf(). It also returns the fractional difference of the rounded number as a second result.

1print(math.floor(3.3)) -- 3
2print(math.floor(-3.3)) -- -4
3print(math.ceil(3.3)) -- 4
4print(math.ceil(-3.3)) -- -3
5print(math.modf(3.3)) -- 3 0.2999999999999998
6print(math.modf(-3.3)) -- -3 -0.2999999999999998
7

Random Number Generator

You can generate random numbers using the math.random() function. There are three ways to call this function.

  • Calling without arguments, math.random(), returns a pseudorandom float with a uniform distribution in the interval of [0, 1).
  • Calling with one argument, math.random(), returns a pseudorandom integer with a uniform distribution in the interval of [1, n].
  • Calling with two arguments, math.random(l, u), returns a pseudorandom integer in the interval of [l, u].