2

i am trying to format a string to a number with NSNumberFormatter. My code looks like this:

var displayValue: Double? {
    get {
        let i = "4.2"
        return NSNumberFormatter().numberFromString(i)?.doubleValue
    }
}

The property is always nil, what am i missing?

www40
  • 285
  • 1
  • 7
  • 19
  • 3
    Probably a locale issue. Does it work with "4,2" ? – Martin R Jun 13 '15 at 20:40
  • 2
    Possible duplicate of http://stackoverflow.com/questions/22413605/nsnumberformatter-numberfromstring-decimal-number (which is written in Objective-C, but the problem and the solution are identical in Swift). – Martin R Jun 13 '15 at 20:43
  • Yes, it works with ",", thanks! – www40 Jun 13 '15 at 20:44

1 Answers1

-1

Martin R! is right it was a locale issue "4,2" works fine, thanks!

www40
  • 285
  • 1
  • 7
  • 19