A number in Lua is a double precision floating point number (or just double). For instance:
In Lua, numbers can range from -1.7 × 10308 to 1.7 × 10308 (around 15 digits, positive or negative).
Signed and Unsigned
The sign of the number indicates whether it’s positive or negative. A signed number can be positive or negative, but an unsigned number cannot be negative. In Lua,
-0 is distinct from
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
- Scientific notation — Write a decimal number followed by
e+, then an integer to raise the decimal number to a power of 10. For instance,
12e3is 12 × 10^3 (12,000).
- Hexadecimal (base-16) — Begin the number with
0xfollowed by digits 0–9 or A–F (capitalization ignored). For example,
0xFis 15 and
- Binary (base-2) — Begin the number with
0bfollowed by 0s or 1s, for instance
0b1100(12 in decimal format).
1_234_567is the same as
1234567, both of which are equal to 1,234,567.
Lua math and relational
/articles/Operators|operators can be used on numbers to manipulate and compare them. Mathematical functions such as
math.exp() can be found in the math library and, for bitwise operations, the bit32 library has been back-ported.
In Roblox Lua, there is no technical difference between the following types of numbers. However, number classifications are used in documentation to indicate which kind of number is involved with an API member.
int number type refers to a number without a fractional portion (integer) like 0, 60, or -42. Properties and functions that expect integers may automatically round or raise errors when provided with non-integers.
When working with integers in Lua, note the following:
- The fractional portion of a number can be trimmed by rounding down with
- You can determine if a number is an integer by comparing
math.floor(x) == x.
- To round a number to the nearest integer (half up), use
math.floor(x + 0.5).
int64 number type refers to a signed 64-bit integer. This type is often used for methods which use ID numbers from the Roblox website, such as
MarketplaceService/PromptPurchase|MarketplaceService:PromptPurchase() — in these cases, a signed 64-bit integer is expected (-263 to 263 - 1).
float number type refers to a single-precision (32-bit) floating point number. This type isn’t as precise as normal numbers, but the difference typically won’t matter.
You can determine if a value is a number by using
typeof(x), both of which will return the string
number when provided with a number: