Integers and Floats
How Lua represents numbers.
Integers and Floats is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Numbers in Lua
In Lua, the number type holds all kinds of numeric values. You use numbers to count things, measure values, and do math.
Starting with Lua 5.3, numbers come in two flavors: integers (whole numbers) and floats (numbers with a decimal point).
Let's print a couple of numbers to begin.
print(10)
print(3.14)What Is an Integer?
An integer is a whole number with no fractional part, like 0, 42, or -7.
When you write a number without a decimal point, Lua treats it as an integer.
5is an integer-100is an integer
local apples = 5
print(apples)What Is a Float?
A float (floating-point number) can represent fractional values, like 3.14 or 0.5.
Any number you write with a decimal point is a float, even 2.0.
Floats let you store measurements and results of division.
local price = 2.5
print(price)Checking the Subtype
Both integers and floats share the type name "number". To see which subtype a value has, use math.type().
It returns the string "integer", "float", or nil if the value is not a number.
print(math.type(10))
print(math.type(3.14))type() vs math.type()
The type() function tells you the general type. For any number it returns "number".
The math.type() function digs deeper and tells you integer vs float. Use the right tool for the job.
print(type(7))
print(math.type(7))Division Always Makes a Float
The normal division operator / always produces a float, even when the numbers divide evenly.
So 10 / 2 gives 5.0, not 5. Notice the .0 in the output.
print(10 / 2)
print(math.type(10 / 2))Floor Division
Use the floor division operator // when you want a whole-number result. It divides and rounds down toward negative infinity.
If both operands are integers, the result is an integer.
print(7 // 2)
print(math.type(7 // 2))Floats Can Hold Whole Values
A float can store a whole number too. 4.0 equals 4 in value, but its subtype is still float.
Lua considers 4 == 4.0 to be true because they have the same numeric value.
print(4 == 4.0)
print(math.type(4.0))Converting Float to Integer
You can turn a float into an integer with math.tointeger(). It returns the integer if the float has no fractional part, otherwise nil.
This is handy when an API needs a true integer.
print(math.tointeger(8.0))
print(math.tointeger(8.5))Scientific Notation
Lua lets you write very large or very small floats using scientific notation with e.
1e3 means 1 times 10 to the power 3, which is 1000.0. The result is always a float.
print(1e3)
print(2.5e-2)Integer Limits
Lua integers are stored in 64 bits, so they have a huge but finite range. The constants math.maxinteger and math.mininteger show the bounds.
Floats can represent larger magnitudes but lose exact precision for big whole numbers.
print(math.maxinteger)
print(math.mininteger)Quick Check
Test what you have learned about integers and floats.
Recap
You learned that Lua numbers come in two subtypes: integers (whole numbers) and floats (decimals).
Use math.type() to tell them apart, / always yields a float, and // can keep things integer. math.tointeger() converts when possible.
Next, you will explore arithmetic operators in depth.
Frequently asked questions
Is the “Integers and Floats” lesson free?
Yes — the full text of “Integers and Floats” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Integers and Floats”?
How Lua represents numbers. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Integers and Floats” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integers and Floats
- Arithmetic Operators
- The math Library
- Number Formatting