{

How to Convert String to/from Double in swift with example


This article talks about multiple ways to convert String to Double or String to Double in Swift Language.

String and Double are different data types and hold different values.

When we want to perform mathematical calculations on strings that contain double numbers, we must convert string to double.

if a user enters a numeric value in the text box of a web application form, this will be received as input as a string.

For example, a string contains the numbers 134.434 and needs this value to convert to double.

The string is a primitive type that contains a group of characters or numbers enclosed in double quotes

double is a primitive type that contains numeric values.

How to convert String to Double in Swift with example

There are multiple ways we can convert String to Double types in swift

  • use the Double constructor.

Pass a string that contains the numeric value to the double constructor and returns the double value.

import Foundation;
var str = "12.67"

let number1: Double? = Double(str)
print(type(of: number1)) // Optional<Double>

print(type(of: str)) // String
print(str);

Output:

Double
String
12.67
  • use atof method atof() method takes a string, and converts it into a double-precision floating value. Here is an example
import Foundation;
var str = "12.67"
var number1 = atof(str);
print(type(of: number1)) // Double

print(type(of: str)) // String
print(str);

Output:

Double
String
12.67
  • use NSString doubleValue This example converts String to NSString and calls the doubleValue property. Here is an example
import Foundation;
var str = "12.67"
 let number = (str as NSString).doubleValue
print(type(of: number));// Double
print(type(of: str)) // String
print(str);

Output:

Double
String
12.67

How to Convert Double to String in swift?

  • using the String format

The string has a format specifier that takes the format of “%f” with value and returns the string object.

Here is an example

import Foundation;

let number: Double = 123.289

print(type(of: number));// Double
print(number);
let str: String = String(format: "%f", number)

print(type(of: str)) // String
print(str);

Output:

Double
123.289
String
123.289
  • using String constructor String constructor accepts a double value and returns a string value.
String(Double d)

Here is an example

import Foundation;

let number: Double = 123.289

print(type(of: number));// Double
print(number);
let str: String = String(number)

print(type(of: str)) // String
print(str);
  • use String interpolation syntax double value is enclosed in () and escaped by \ character and include this in double-quotes(""). Here is a string interpolation syntax.
"\(doublevalue)"

Example

import Foundation;

let number: Double = 123.289

print(type(of: number));// Double
print(number);
let str: String = "\(number)"

print(type(of: str)) // String
print(str);

  • using double description

The Double type has a description property that returns the string version of the numeric value.

import Foundation;

let number: Double = 123.289

print(type(of: number));// Double
print(number);
let str: String = number.description

print(type(of: str)) // String
print(str);

Conclusion

To summarize, learned multiple ways to convert string to/from double in swift language with examples.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.