Search code examples
iosnsnumberformatter

Format 9999999 into 9,999,999 (comma after every third digit) using NSNumberFormatter


Similar to comma separated thousand NSString stringWithFormat but with a specific use case.

How do I insert a comma (,) after every third digit?

Some cases:

  • 9999999 → 9,999,999
  • 1234 → 1,234
  • 3432423455435435 → 3,432,423,455,435,435

The results are expected regardless of locale or any other circumstance.


Solution

  • What I needed was not something that would care for the locale of a user, and with that it was just easier to do some string juggling. If someone cares I could dig up that code...

    If you however DO care for locales, go with the other answer of Dave DeLong. Technically however it does not answer the question.