8

I have an NSInteger (say with value 60000), when I convert it to string however, I want to get "60,000" instead of "60000". Is there some method to do it? Thanks.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1903992
  • 465
  • 2
  • 8
  • 16
  • duplicated with http://stackoverflow.com/questions/2233824/how-to-add-commas-to-number-every-3-digits-in-objective-c – Nazir Mar 06 '16 at 15:04

4 Answers4

19

Use a number formatter:

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setNumberStyle:NSNumberFormatterDecimalStyle]; // to get commas (or locale equivalent)
[fmt setMaximumFractionDigits:0]; // to avoid any decimal

NSInteger value = 60000;

NSString *result = [fmt stringFromNumber:@(value)];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Hi, thanks for your reply. It works. Two small questions though. I see this "locale" being mentioned all the time, what is it anyway? And also, in your approach, how does it know that it should set commas after every 3 digits, instead of 2 for example? and finally, will it also work for iOS4.3? Thanks. – user1903992 Jan 09 '13 at 07:58
  • 1
    See the docs for `NSLocale` for a description of locale. In short it deals with language and formatting differences around the world. Some countries will show the 600000 as 600,000. Some will show it as 600.000 or 600 000. My answer applies for all versions of iOS since 2.0. See the docs for `NSNumberFormatter` - specifically those related to grouping. The default is 3 digits. – rmaddy Jan 09 '13 at 08:02
  • Thanks for your response. (Can't submit short answers here though :)). – user1903992 Jan 09 '13 at 08:09
3

You can use a number formatter:

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]];
Andrew Tetlaw
  • 2,669
  • 22
  • 27
2

Try this,

NSString *numString = [NSString stringWithFormat:@"%d,%d",num/1000,num%1000];
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
-1

Use the NSNumberFormatter for formatting numeric data to a localized string representation.

int aNum = 60000;
NSString *display = [NSNumberFormatter localizedStringFromNumber:@(aNum)
                                                     numberStyle:NSNumberFormatterCurrencyStyle];

On doing this,you will get '$60,000.00'

after that,you can remove the sign of $ and '.'(decimal) by doing this..

 NSString *Str = [display stringByReplacingOccurrencesOfString:@"$" withString:@""];

 NSString *Str1 = [Str stringByReplacingOccurrencesOfString:@"." withString:@""];

NSString *newString = [Str1 substringToIndex:[Str1 length]-1];

NSString *newString1 = [newString substringToIndex:[newString length]-1];

'newString1' will give you the required result.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
  • 1
    This code is overly complicated and it will fail in many cases. This won't work for locales using a currency symbol other than `$`. It also fails for locales that use a decimal separator other than `.`. It also fails for locales that don't use 2 decimal places for currency. – rmaddy Jan 09 '13 at 08:46
  • What does that mean? What situation? Are you proposing that the answer is to write a whole set of code to handle all of the different locales? – rmaddy Jan 09 '13 at 15:29