Search code examples
iosswiftnumberformatter

NumberFormatter works on simulator but not on device swift


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


Solution

  • 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")