Number Formatting
Display numbers nicely.
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))All lessons in this course
- Integers and Floats
- Arithmetic Operators
- The math Library
- Number Formatting