4

I want to format the number separated by comma for every 3 digits. For ex:12346778, I want this number as 12,346,778 Can anyone please Suggest sample code for this. Thanks in Advance.

Madan Mohan
  • 8,764
  • 17
  • 62
  • 96
  • possible duplicate of [How to add commas to number every 3 digits in Objective C?](http://stackoverflow.com/questions/2233824/how-to-add-commas-to-number-every-3-digits-in-objective-c) – Gilles 'SO- stop being evil' Jun 21 '11 at 12:37

4 Answers4

8
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];  
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
John Rutherford
  • 10,704
  • 7
  • 30
  • 32
Swapna
  • 2,245
  • 2
  • 19
  • 22
2

Use NSNumberFormatter

Use the method

- (void)setGroupingSeparator:(NSString *)string
KingofBliss
  • 15,055
  • 6
  • 50
  • 72
2
NSInteger currency = 123456789;
NSNumberFormatter *num = [[NSNumberFormatter alloc] init];
[num setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [num stringFromNumber:[NSNumber numberWithInt:currency]];
[num release];
Naveen Thunga
  • 3,675
  • 2
  • 24
  • 31
0

I have a solution for you. Very easy for understand!

NSNumberFormatter *mVolFormatter = [[NSNumberFormatter alloc] init];

[mVolFormatter setPositiveFormat:@"#,##0"];
[mVolFormatter setZeroSymbol:@"-"];
long Vol = 5000000;
NSString *strYouNeed = [mVolFormatter stringFromNumber:[NSNumber numberWithInt:vol]];

and =>> strYouNeed = @"5,000,000"

Check my code!!!

Linh Nguyen
  • 2,006
  • 1
  • 20
  • 16