Precision String Formatting in Swift: Secrets and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Precision string format specifier in Swift: how to control number output precision



Hello, colleague! If you've ever needed to output a number with a specific number of decimal places in Swift, you've probably noticed that the classic String(format:) doesn't always behave as expected. Especially if you come from the world of C or Objective-C, where printf is sacred. In this article, we'll break down how the precision string format specifier works in Swift, why it sometimes "breaks," and how to use it correctly. Let's go!



What is a precision specifier and why do you need it?



Precision specifier is a part of the format string that determines how many decimal places (or the total number of digits) should be displayed. In Swift, as in C, it is written after a dot inside the format specifier. For example, %.2f means "output a floating-point number with two decimal places."



But there's a nuance: Swift uses Core Foundation for string formatting, and its behavior may differ from what you're used to in C. This is especially true when working with Double and Float.



Let's immediately look at a simple example that often appears in Stack Overflow questions:



let pi = Double.pilet formatted = String(format: "%.2f", pi)print(formatted) // Outputs: "3.14"


Everything works like clockwork. But as soon as we start experimenting with more complex formats, surprises begin.



Main types of precision specifier in Swift



Swift supports the following specifiers that affect precision:



  • %f — floating-point number. Precision specifies the number of decimal places.
  • %e — scientific notation. Precision is the number of digits after the decimal point.
  • %g — chooses between %f and %e depending on the value. Precision is the total number of significant digits.
  • %d / %i — integers. Precision is the minimum number of digits (padded with leading zeros).
  • %s — strings. Precision is the maximum number of characters to output.


But the most interesting part begins when we try to apply precision to Double or Float via String(format:).



Common problem: "Why doesn't precision work?"



One of the most frequent complaints on Stack Overflow: "I write String(format: "%.2f", myDouble), but I get an unexpected result." Let's break down a typical scenario:



let value: Float = 1.23456789let result = String(format: "%.4f", value)print(result) // May output "1.2346" or "1.2345" depending on compilation


Why? Because Float has a precision of about 6-7 significant digits. If you pass 1.23456789, it gets rounded to 1.234568 (or thereabouts) before it even reaches the format. And the precision in %.4f is then applied to this rounded value.



To avoid confusion, always use Double for floating-point numbers when high precision is needed:



let value: Double = 1.23456789let result = String(format: "%.4f", value)print(result) // Reliably outputs "1.2346"


Advanced examples: how to control precision dynamically



Sometimes precision needs to be set dynamically rather than hardcoded. Swift allows this in two ways:



Method 1: using * (asterisk)



In the format specifier, you can use * instead of a number, and the precision will be taken from the following

Blogs

Book Recommendations