Convert String to Float in Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a string to a floating-point number in Swift is usually straightforward, but the details matter once user input, locale rules, or invalid data enter the picture. Swift gives you safe initializers for Float and Double, and both return optionals so conversion failures are explicit. The right solution depends on whether you control the string format or need to parse human-entered numbers.
Use the Built-In Initializers
If the string already uses a standard decimal format, Swift can parse it directly.
You can do the same with Double:
The important part is that the result is optional. If parsing fails, you get nil instead of a silent wrong value.
Prefer Double Unless You Need Float
Swift supports both Float and Double, but Double is usually the better default because it has more precision.
Use Float when:
- an API specifically expects
Float - memory layout matters in numeric arrays
- you are working with frameworks that use
Floatheavily
For general app code, Double is the more common choice.
If the ultimate destination type is Float, you can convert after successful parsing.
Trim Whitespace Before Parsing
Strings from text fields, files, or network payloads often contain leading or trailing spaces. Trimming them makes parsing more reliable.
This is a small step, but it removes a common source of avoidable parsing failures.
Handle Invalid Input Safely
Do not force-unwrap a parsed number unless the input is guaranteed by your own code. User input and external data are not guaranteed.
Unsafe:
Safe:
The safe version makes the failure path explicit and prevents runtime crashes.
Use NumberFormatter for Locale-Aware Parsing
The simple initializers expect a predictable machine-style number format, usually with . as the decimal separator. Human-entered values may use locale-specific separators instead.
For example, some locales write 12,5 instead of 12.5. NumberFormatter is the right tool for that.
If your app accepts user-facing numeric input, locale-aware parsing is often more correct than calling Double(text) directly.
Converting From Text Fields
A very common case is converting input from UITextField or TextField content.
In real apps, combine this with validation and user feedback rather than failing silently.
Common Pitfalls
The biggest mistake is force-unwrapping the conversion result. A single invalid string then becomes a crash instead of a validation error.
Another issue is choosing Float automatically when Double would be a better default. Unless an API requires Float, Double usually gives you fewer precision surprises.
Locale is another subtle problem. A string that looks correct to the user may still fail with Double(text) if the decimal separator does not match the machine-oriented format expected by the initializer.
Finally, remember to trim whitespace when parsing raw input. Many “mysterious” failures are just unclean strings.
Summary
- Use
Float(text)orDouble(text)for standard numeric strings. - Treat the result as optional and handle failure explicitly.
- Prefer
Doubleunless you specifically needFloat. - Trim whitespace before parsing user or external input.
- Use
NumberFormatterwhen the input format depends on locale rules.

