Madan Mohan
Madan Mohan

Reputation: 8820

how to format the number with commas in iPhone programing

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.

Upvotes: 4

Views: 3111

Answers (4)

Linh Nguyen
Linh Nguyen

Reputation: 2036

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!!!

Upvotes: 0

Swapna
Swapna

Reputation: 2235

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];  
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

Upvotes: 8

Naveen Thunga
Naveen Thunga

Reputation: 3675

NSInteger currency = 123456789;
NSNumberFormatter *num = [[NSNumberFormatter alloc] init];
[num setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [num stringFromNumber:[NSNumber numberWithInt:currency]];
[num release];

Upvotes: 2

KingofBliss
KingofBliss

Reputation: 15115

Use NSNumberFormatter

Use the method

- (void)setGroupingSeparator:(NSString *)string

Upvotes: 2

Related Questions