Swift
Double
String Conversion
Programming
iOS Development

Swift double to string

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Converting a Double to a String in Swift is something you will do constantly, whether you are displaying a price label, formatting sensor data, or logging debug values. Swift provides several approaches, each with different levels of control over precision and formatting. Choosing the right one depends on whether you need a quick conversion or precise control over decimal places, locale, and presentation style.

String Interpolation

The simplest way to turn a Double into a String is string interpolation. Swift automatically calls the value's description property and embeds it in the string:

swift
let pi = 3.14159265
let message = "The value is \(pi)"
print(message)  // "The value is 3.14159265"

This is convenient for debugging, but you have no control over how many decimal places appear. The output depends on Swift's default representation of the value.

The String Initializer

You can also use the String initializer directly. This produces the same result as interpolation but is useful when you need a standalone String rather than embedding the value inside a larger string:

swift
let temperature = 98.6
let text = String(temperature)
print(text)  // "98.6"

Both interpolation and the String initializer give you a faithful representation of the Double, but neither lets you specify a fixed number of decimal places.

Controlling Decimal Places with String(format:)

When you need a specific number of decimal places, use String(format:) with a C-style format specifier. The %f specifier formats the number as a fixed-point decimal, and you can prefix the f with .N to set the precision:

swift
1let price = 9.99999
2print(String(format: "%.2f", price))   // "10.00"
3print(String(format: "%.4f", price))   // "10.0000"
4print(String(format: "%.0f", price))   // "10"

Other useful format specifiers include %e for scientific notation and %g which automatically chooses between fixed-point and scientific notation based on the value's magnitude:

swift
1let large = 1500000.0
2print(String(format: "%e", large))   // "1.500000e+06"
3print(String(format: "%g", large))   // "1.5e+06"
4
5let small = 42.5
6print(String(format: "%g", small))   // "42.5"

NumberFormatter for Locale-Aware Output

For user-facing text, especially in apps distributed internationally, NumberFormatter is the right tool. It handles locale-specific differences such as decimal separators (period vs. comma), thousands grouping, and currency symbols:

swift
1let formatter = NumberFormatter()
2formatter.numberStyle = .decimal
3formatter.minimumFractionDigits = 2
4formatter.maximumFractionDigits = 2
5
6let value = 1234567.891
7if let result = formatter.string(from: NSNumber(value: value)) {
8    print(result)  // "1,234,567.89" in US locale, "1.234.567,89" in German locale
9}

For currency formatting, switch to .currency:

swift
1let currencyFormatter = NumberFormatter()
2currencyFormatter.numberStyle = .currency
3currencyFormatter.locale = Locale(identifier: "en_US")
4
5if let formatted = currencyFormatter.string(from: NSNumber(value: 29.99)) {
6    print(formatted)  // "$29.99"
7}

NumberFormatter is heavier to create than a simple String(format:) call, so if you are formatting many values in a tight loop, create the formatter once and reuse it.

Formatted Method (iOS 15+)

Starting with iOS 15 and macOS 12, Swift introduced the .formatted() method that provides a modern, type-safe API:

swift
1let measurement = 3.14159
2print(measurement.formatted(.number.precision(.fractionLength(2))))
3// "3.14"
4
5print(measurement.formatted(.percent))
6// "314.159%"

This API is concise and avoids the need to work with NSNumber or C-style format strings.

Converting Back: String to Double

For completeness, here is how to convert in the other direction. Use the Double initializer, which returns an optional:

swift
1let input = "42.5"
2if let number = Double(input) {
3    print(number)  // 42.5
4} else {
5    print("Invalid number")
6}

Always unwrap the optional safely, since the conversion fails if the string contains non-numeric characters.

Common Pitfalls

  • Using string interpolation for user-facing prices. Interpolation does not respect locale settings. A user in Germany expects 3,14 not 3.14. Use NumberFormatter for any text shown to end users.
  • Creating a new NumberFormatter in every call. NumberFormatter allocation is expensive. Create one instance, configure it, and reuse it across your formatting calls.
  • Assuming Double can represent all decimal values exactly. Double uses binary floating point, so 0.1 + 0.2 does not equal 0.3 exactly. For financial calculations, consider using Decimal instead of Double.
  • Forgetting that String(format:) always uses the POSIX locale. The %f specifier always uses a period as the decimal separator regardless of the device locale, which is correct for data serialization but wrong for display text.
  • Ignoring the optional return from NumberFormatter. The string(from:) method returns an optional String. Force-unwrapping it will crash your app if the formatter encounters an unexpected value. Always use if let or guard let.

Summary

  • Use string interpolation or String(value) for quick debug output where formatting does not matter.
  • Use String(format: "%.Nf", value) when you need a fixed number of decimal places in a locale-independent context.
  • Use NumberFormatter for all user-facing text to ensure correct locale-specific formatting (decimal separators, currency symbols, grouping).
  • On iOS 15 and later, the .formatted() method offers a concise, type-safe alternative to NumberFormatter.
  • Avoid creating formatters repeatedly in performance-sensitive code; instantiate once and reuse.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.