Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate number with commas on a string iOS

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.

like image 276
user1903992 Avatar asked Jan 09 '13 07:01

user1903992


People also ask

How do you put a comma in between numbers?

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)

How to format number with commas js?

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.


1 Answers

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)];
like image 109
rmaddy Avatar answered Sep 28 '22 05:09

rmaddy