Separate number with commas in iOS line

I have an NSInteger (say with a value of 60,000), when I convert however, I want instead of "60,000" from "60,000". Is there any way to do this? Thanks.

+7
source share
4 answers

Use number formatting:

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)]; 
+18
source

You can use number formatting:

 NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]]; 
+3
source

Try it,

 NSString *numString = [NSString stringWithFormat:@"%d,%d",num/1000,num%1000]; 
+2
source

Use NSNumberFormatter to format numeric data into a localized string representation.

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

With this, you will receive $ 60,000.00

after that you can remove the $ sign and the '.' (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 provide you with the desired result.

-one
source

All Articles