Swift double to string
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
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:
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:
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:
For currency formatting, switch to .currency:
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:
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:
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,14not3.14. UseNumberFormatterfor any text shown to end users. - Creating a new NumberFormatter in every call.
NumberFormatterallocation is expensive. Create one instance, configure it, and reuse it across your formatting calls. - Assuming Double can represent all decimal values exactly.
Doubleuses binary floating point, so0.1 + 0.2does not equal0.3exactly. For financial calculations, consider usingDecimalinstead ofDouble. - Forgetting that
String(format:)always uses the POSIX locale. The%fspecifier 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 optionalString. Force-unwrapping it will crash your app if the formatter encounters an unexpected value. Always useif letorguard 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
NumberFormatterfor 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 toNumberFormatter. - Avoid creating formatters repeatedly in performance-sensitive code; instantiate once and reuse.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.