How to convert the string to Double in Swift
In this post we will give you information about How to convert the string to Double in Swift. Hear we will give you detail about How to convert the string to Double in SwiftAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to convert the string to double in Swift.
Converting string to double
To convert a string to a double, we can use the built-in Double() initializer syntax in Swift.
The Double() initializer takes the string as an input and returns the double instance. If the string contains invalid characters or invalid format it returns nil.
Here is an example, that converts the string “13.344” to Double:
let string = "13.344"let myDouble = Double(string) ?? 0print(myDouble)
Output:
13.344
Similarly, we can also use the following syntax to convert it.
import Foundationlet string = "13.344"let myDouble = (string as NSString).doubleValueprint(myDouble)
Hope this code and post will helped you for implement How to convert the string to Double in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs