How to add commas to number every 3 digits in Objective C?

后端 未结 9 737

If I have a number int aNum = 2000000 how do I format this so that I can display it as the NSString 2,000,000?

9条回答
  •  情话喂你
    2020-11-28 03:36

    An easy solution could be this. My answer is almost same like @Nazir's answer but with a small trick.

    double current_balance = 2000000.00;
    
    NSNumberFormatter * formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    //[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];  //if you want for currency with $ sign
    [formatter setMinimumFractionDigits:2]; // Set this if you need 2 digits
    [formatter setMaximumFractionDigits:2]; // Set this if you need 2 digits
    NSString * currency_format =  [NSString stringWithFormat:@"%@", [formatter stringFromNumber:[NSNumber numberWithDouble:current_balance]]];
    

提交回复
热议问题