user69450
user69450

Reputation: 23

Getting unexpected result from NSNumberFormatter

I have the following code:

var num = 0.03829585
let amountNumber = NSNumber(double: num)
let nFormatter = NSNumberFormatter()
nFormatter.numberStyle = .DecimalStyle
nFormatter.roundingMode = .RoundHalfUp
nFormatter.maximumFractionDigits = 8
var numStr = nFormatter.stringFromNumber(amountNumber)!

The expected result of numStr is "0.03829585". That is what I get with my test devices, but on a small number of my TestFlight users' devices I get "0,03829585". Why is that? How can this be fixed so that I always get the right string representation?

Upvotes: 2

Views: 64

Answers (1)

Stefan Scoarta
Stefan Scoarta

Reputation: 791

NSNumberFormatter takes the locale in the consideration. The devices can have set their locale to a country where the people use a comma in the place of the point. This is a feature, not a bug. People use NSNumberFormatter specialy for the localisation feature. If you want the point to be everywhere you can simply transform that double into a string (don't do it). That doesn't only take care of the point/comma problem, it takes care for the digits, for example persian digits are different then the digit we know

Upvotes: 5

Related Questions