Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use String Formatting in Swift
Written by Team Kodeco

The String(format:) method in Swift is used to create a new string by using a specified format string and a list of arguments. The format string contains placeholders for the values that will be passed in as arguments and can include special format specifiers that control how the values are formatted.

The basic syntax for using the String(format:) method is:

let formattedString = String(format: formatString, arguments...)

Where “formatString” is the format string that contains the placeholders and format specifiers, “arguments” is a list of values that will be used to replace the placeholders in the format string.

The placeholders in the format string are represented by the “%” character followed by a format specifier.

To understand format specifiers, let’s start with an example:

import Foundation

let number = 3.14159265
let formattedString = String(format: "%5.2f", number)
print(formattedString) // Output: " 3.14"

Here the format specifier is “%5.2f”. This is broken into three parts:

  1. Width: The first number in the format specifier is the width, it represents the minimum number of characters to be printed for the value. If the value is shorter than the width, it’ll be padded with whitespace on the left or right, depending on the justification specified. In this example, the number “5” is the width, it tells String(format:) to include at least 5 characters. 3.14 is only 4 characters, so it pads a character of whitespace on the left.
  2. Precision: After the width, there is a dot and then the precision. The precision represents the number of digits to appear after the decimal point for floating-point numbers. In this example, this is set to 2, so there it only includes two digits after the decimal point.
  3. Type: After the width and precision, you include a character that represents the type you want to format the string to. In this example, this is set to f which represents hexadecimal.

Here is a table of some of the most common types you can use in your format specifier:

  • %d, %i: Integer.
  • %o: Octal.
  • %x, %X: Hexadecimal.
  • %f: Floating-point.
  • %e, %E: Floating-point in exponential notation.
  • %g, %G: Floating-point in either exponential or decimal notation, depending on the value.
  • %%: Literal % character.

Examples

To better understand format specifiers, here are a few more examples:

// Width examples
String(format: "%3d", 9)    // "  9"
String(format: "%3d", 99)   // " 99"
String(format: "%3d", 999)  // "999"
String(format: "%3d", 9999) // "9999"

// Precision examples
String(format: "%.3f", 3.14159265) // 3.142
String(format: "%.2f", 3.14159265) // 3.14
String(format: "%.1f", 3.14159265) // 3.1
String(format: "%.3f", 3.0)        // 3.000
String(format: "%.2f", 3.0)        // 3.00
String(format: "%.1f", 3.0)        // 3.0

// Type examples
String(format: "%d", 32)   // 32
String(format: "%o", 32)   // 40
String(format: "%x", 32)   // 20
String(format: "%f", 32.0) // 32.000000
String(format: "%e", 32.0) // 3.200000e+01
String(format: "%g", 32.0) // 32
String(format: "%%")       // %

Note: The number of arguments passed to the method must match the number of placeholders in the format string, otherwise, the program will crash.

© 2024 Kodeco Inc.