I have an NSInteger (say with value 60000), when I convert it to string however, I want to get "60,000" instead of "60000". Is there some method to do it? Thanks.
A comma is placed every third digit to the left of the decimal point and so is used in numbers with four or more digits. Continue to place a comma after every third digit. For example: $1,000,000 (one million dollars)
You can use the toLocaleString() method to comma-separate thousands in a number for a given localization, such as 'en-US'. You can also use the Intl. NumberFormatter object to format a number to a specific localization. Lastly, you can also build your own custom function to get the job done.
Use a number formatter:
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)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With