Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Lambrecht
iOS Development Techdegree Student 5,302 PointsSwift: UITextfield --> double with comma as decimal seperator (0,7) instead of (0.7)
Hi, ive been trying to figure this thing out and google didnt have the answer so her goes!
im making a simple app to calculate some values.
Therefore the user needs to enter a value for instance it could be 1.65. I need this to be converted in to a double, which is almost working. besides it gives me the double 1.00.
It does this because in my country when we write the number it would be 1,65 - Swift then only uses the first number 1 and converts that to a double, leaving the rest.
I need a solution that gives the user the possibility to input something like 1,65 Giving me the Double value: 1.65
Can anyone help me with that?
1 Answer
David Papandrew
8,386 PointsCheck out the solutions on this stackoverflow post: http://stackoverflow.com/questions/28313871/swift-playground-how-to-convert-a-string-with-comma-to-a-string-with-decimal
For a quick and dirty solution, you could use the replacingOccurences string method (you need to import Foundation to access this) and then cast the string as a Double.
Something like this:
import Foundation
let commaValue = "8,35"
let decimalValue = Double(commaValue.replacingOccurrences(of: ",", with: "."))
But NSNumberFormatter (see stackoverflow link) is probably the better route. Good luck.
Daniel Lambrecht
iOS Development Techdegree Student 5,302 PointsWow thanks!
kjvswift93
13,515 Pointskjvswift93
13,515 PointsWhere are you from?