Search code examples
iosnsnumberformatter

Formatting a number with Comma same as iphone calculator Formatting


I want to format the number separated by comma, same as the iphone calculator format , i just use the following code but it shows the Wrong format for example if i enter 100000 it shows 1,00,000 . the correct format is 100,000 Please help

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

Solution

  • You need to add grouping Size to 3 :

    NSNumberFormatter *formatter1 = [NSNumberFormatter new];
    [formatter1 setUsesGroupingSeparator: YES];
    [formatter1 setGroupingSeparator: @","];
    [formatter1 setGroupingSize: 3];
    
    NSString *numberString = [formatter1 stringFromNumber: [NSNumber numberWithInteger: 1000000]];