1

The issue I have observed on 2 identical iphones , 6s , IOS 10.2

Say for example I have a string like this "7.22 ".

I want to convert it to a double value so I do the following.

let myDoubleString = "7.22 "
let myDouble = Double(myDoubleString)

On one device this works completely fine. On the other device my double is nil. Note this problem only occurs if myDoubleString has a space at the end.

The short solution to the problem is "don't include spaces" but I'm really curious why the conversion would fail between different devices of the same type and OS version

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
dubbeat
  • 7,706
  • 18
  • 70
  • 122
  • Do both devices have same `locale`(Language&Region) in `Settings`? – user28434'mstep Feb 15 '17 at 11:53
  • Probably not actually. Their in different time zones / Countries – dubbeat Feb 15 '17 at 12:06
  • Different countries could mean different `regions` in setting, and different `regions` mean different `locales` and different `locales` means different format for parsing numbers. – user28434'mstep Feb 15 '17 at 12:48
  • Ok, I was able to reproduce the error on my device by changing my region. I still don't entirely understand how a different locale would cause the problem but it does appear to be the case. If you'd like to add an answer I can mark it as accepted – dubbeat Feb 15 '17 at 13:12
  • @dubbeat http://stackoverflow.com/a/30154625/2303865 – Leo Dabus Feb 15 '17 at 14:30
  • 2
    @dubbeat `…I still don't entirely understand how a different locale would cause the problem…`: `Double(_:String)` is using current locale settings (decimal separator character, thousand separator, etc) when parsing double from a string. So if you really need to have consistent parsing everywhere use `NumberFormatter` with fixed `locale`. Yeah, i should move it in answer with examples. – user28434'mstep Feb 15 '17 at 14:57

0 Answers0