Home > front end >  NumberFormatter works on simulator but not on device swift
NumberFormatter works on simulator but not on device swift

Time:08-19

Separate thousands with commas. On the simulator, everything works perfectly, but on the device, for some reason, the same code, instead of a comma, puts a space.

This is a simulator example

enter image description here

This is a device example

enter image description here

CodePudding user response:

You are wrong, the formatter works correctly both on the simulator and on the device - each just uses a different locale. If this is a number displayed to the user then it's completely correct that the number is formatted in the user's locale.

If you want to use a specific locale everywhere, you have to set it explicitly, e.g.:

formatter.locale = Locale(identifier: "en_US")

CodePudding user response:

You need to specify the formatter.locale otherwise the NumberFormatter will get the default from device.

https://developer.apple.com/documentation/foundation/numberformatter/1416967-locale

  • Related