Swift Academy · Lesson

Formatting Numbers in Strings

Control decimals and padding with formatters.

Lesson 2 of 413 steps

Formatting Numbers in Strings is a free Swift Academy lesson on CoddyKit. This is lesson 2 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 Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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))))
Free to start

Learn Swift 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
122
Lessons
409

Frequently Asked Questions

Is the “Formatting Numbers in Strings” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Formatting Numbers in Strings”, 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 Swift Academy course includes 4 lessons in total.

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

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

  1. Basic String Interpolation
  2. Formatting Numbers in Strings
  3. Multiline String Literals
  4. Custom String Interpolation
← Back to Swift Academy