Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I am making a very simple beginner project where I receive the data from a textField and store it in a Double var. I have tried the .toDouble() but it doesn´t seem to exist. Any help please?
You can simply put the string in as a parameter when creating a double!
let double = Double(textView.text!)
Add a comment
Add the following extension and use .toDouble()
.toDouble()
extension String { func toDouble() -> Double? { return NSNumberFormatter().numberFromString(self)?.doubleValue } }
Like
var someString = “30.23" var someDouble = someString.toDouble()
extension String { var doubleValue: Double { return Double(self) ?? 0 } }
Usage:
let inputString = "32.1" let myDouble = inputString.doubleValue // 32.1
if let doubleValue = Double(textView.text!) { } else { print("Not a valid number: \(textView.text!)") }
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.