Lua Academy · Lesson

Number Formatting

Display numbers nicely.

Lesson 4 of 413 steps

Number Formatting is a free Lua Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

Why Format Numbers?

Raw numbers do not always print the way you want. A price might show as 2.5 when you want 2.50, or a long float fills the screen with digits.

Number formatting lets you control exactly how numbers appear in text.

print(2.5)
print(1 / 3)

Meet string.format

The main tool for formatting is string.format(). You give it a template with placeholders and the values to fill in.

A placeholder starts with %, like %d for an integer or %f for a float.

print(string.format("Score: %d", 42))

Formatting Integers with %d

The %d placeholder formats a value as a decimal integer.

It is perfect for counts, scores, and IDs that should never show a decimal point.

local lives = 3
print(string.format("Lives: %d", lives))

Formatting Floats with %f

The %f placeholder formats a float. By default it shows six digits after the decimal point.

That is often more than you need, so you usually set the precision yourself.

print(string.format("%f", 3.14))

Controlling Decimal Places

Write %.2f to show exactly two decimal places. The number after the dot is the precision.

This is the classic way to format money values cleanly.

local price = 2.5
print(string.format("$%.2f", price))

Rounding with Formatting

Formatting also rounds the value to the requested precision. %.1f on 3.456 rounds to 3.5.

Note this only affects the displayed text, not the original number.

print(string.format("%.1f", 3.456))

Setting Field Width

A number before the dot sets the minimum field width. %5d pads the integer to at least five characters using spaces.

This helps align numbers in columns.

print(string.format("[%5d]", 42))
print(string.format("[%-5d]", 42))

Padding with Zeros

Add a 0 after the % to pad with zeros instead of spaces. %03d turns 7 into 007.

This is great for clock displays and zero-padded IDs.

print(string.format("%02d:%02d", 9, 5))

Mixing Numbers and Text

You can combine several placeholders in one template, including %s for strings.

The values are matched to the placeholders in order, left to right.

print(string.format("%s has %d points", "Ada", 95))

Concatenation with tostring

For simple output you can also turn a number into text with tostring() and join it using the .. operator.

This gives less control than string.format but is quick for basic messages.

local n = 7
print("Value is " .. tostring(n))

Hex and Other Bases

string.format can also show integers in other bases. Use %x for lowercase hexadecimal and %X for uppercase.

This is handy when working with colors or low-level values.

print(string.format("%x", 255))
print(string.format("%X", 255))

Quick Check

Test your number formatting skills.

Recap

You learned to format numbers with string.format() using placeholders like %d, %f, and %x.

Precision (%.2f), field width (%5d), and zero padding (%03d) give you precise control, while tostring() with .. handles quick cases.

You have completed Lua Numbers and Math. Great work!

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Number Formatting” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Number Formatting”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

What will I learn in “Number Formatting”?

Display numbers nicely. 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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Number Formatting” 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

  1. Integers and Floats
  2. Arithmetic Operators
  3. The math Library
  4. Number Formatting
← Back to Lua Academy