Formatting Numbers in Strings
Control decimals and padding with formatters.
Formatting Numbers in Strings is a free Swift Academy lesson on CoddyKit — lesson 2 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 Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Formatting Numbers
Raw interpolation of a Double can print many decimals. To control the output you can use String(format:) with C-style format specifiers.
let pi = 3.14159265
let text = String(format: "%.2f", pi)
print(text)Controlling Decimal Places
The specifier %.2f means a floating-point number with 2 digits after the decimal point. Change the number to change precision.
let value = 2.0 / 3.0
print(String(format: "%.0f", value))
print(String(format: "%.3f", value))Formatting Integers
Use %d for integers. You can also pad with width specifiers like %5d.
let n = 42
print(String(format: "Value: %d", n))
print(String(format: "[%5d]", n))Zero-Padding Numbers
A width prefixed with zero pads with leading zeros, handy for fixed-width identifiers or times.
let hour = 9
let minute = 5
print(String(format: "%02d:%02d", hour, minute))Rounding via Formatting
The %f specifier rounds to the requested decimal places, so formatting can serve as display rounding.
let amount = 19.996
print(String(format: "%.2f", amount))Combining Format and Interpolation
You can format a value first, then embed the result with normal interpolation for readable sentences.
let temp = 21.6789
let shown = String(format: "%.1f", temp)
print("Temperature: \(shown) degrees")Percentages and Symbols
To print a literal percent sign with String(format:) you write two percent signs.
let rate = 0.075
print(String(format: "%.1f%% rate", rate * 100))FloatingPointFormatStyle Basics
Modern Swift offers formatted() with a FloatingPointFormatStyle, a type-safe alternative to format strings.
let value = 1234.5678
print(value.formatted(.number.precision(.fractionLength(2))))Format Style Precision
The .precision(.fractionLength(n)) modifier sets how many fractional digits appear, similar to %.nf.
let x = 3.14159
print(x.formatted(.number.precision(.fractionLength(0))))
print(x.formatted(.number.precision(.fractionLength(3))))Grouping with Format Style
Number format styles can also add grouping separators for large numbers in a locale-aware way.
let big = 1234567.0
print(big.formatted(.number.precision(.fractionLength(0))))Choosing a Formatting Approach
Use String(format:) for quick C-style control, or formatted() for type-safe, locale-aware output in modern code.
let price = 9.5
print(String(format: "%.2f", price))
print(price.formatted(.number.precision(.fractionLength(2))))Quick Check
Recall what a format specifier controls.
Recap: Formatting Numbers in Strings
You learned to control numeric output with String(format:) specifiers like %.2f, %d, and zero-padding %02d, and to use the modern type-safe formatted(.number.precision(...)) style for locale-aware results.
let v = 7.0 / 3.0
print(String(format: "%.2f", v))
print(v.formatted(.number.precision(.fractionLength(2))))Frequently asked questions
Is the “Formatting Numbers in Strings” lesson free?
Yes — the full text of “Formatting Numbers in Strings” is free to read here on the web, and the Swift 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 Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Formatting Numbers in Strings”?
Control decimals and padding with formatters. You practise Swift 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 Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Formatting Numbers in Strings” 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 Swift Academy lesson?
Yes. Every Swift 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
- Basic String Interpolation
- Formatting Numbers in Strings
- Multiline String Literals
- Custom String Interpolation