Cocoa: Convert number to comma-separated NSString

Contributor Icon Contributed by qmchenry Date Icon January 30, 2010  
Tag Icon Tagged: C programming  

At some point in your Mac, iPhone, or iPad development you may find the need to express a long number (1000000000) as a comma-separated string (1,000,000,000) to make life easier on your users. The NSNumberFormatter class is a rich tool for converting numbers to strings supporting different types of currencies and localizations. It’s also the perfect class to leverage for our comma-ing task.

First of all, the NSNumberFormatter class works on NSNumber objects, so we need to convert our number to a NSNumber if it’s not there already.
NSNumber *number = [NSNumber numberWithInt:1000000000];

NSNumber also supports floating point values (numberWithFloat) and the regular gang of other number formats.

With our NSNumber in hand, we can get on with the good stuff. NSNumberFormatter supports grouping of numeric digits into arbitrary length groups (we want groups of three) and separating the groups with arbitrary strings (we want to use a comma (@”,”) but we could use any string). Here’s the code that makes our string:

NSNumberFormatter *frmtr = [[NSNumberFormatter alloc] init];
[frmtr setGroupingSize:3];
[frmtr setGroupingSeparator:@","];
[frmtr setUsesGroupingSeparator:YES];
NSString *commaString = [frmtr stringFromNumber:number];

Read more about NSNumberFormatter’s crazy tricks here. It can also do cool things like spelling out a number like 42 into forty-two and handling significant digits.

 
  • heyatullah

    HOW I RUN XP BASED PROGRAM IN WINDOWS7

  • Susan

    What about all the countries that don't use “3 digits” and don't use “commas”????

    How would you write code that worked e-v-e-r-y-w-h-e-r-e, not just in small parts of the world?

  • Aaron

    Well, I have thought long and hard about this particular issue — the one with commas versus periods. I have come to the conclusion that the United States, and any other country that uses the comma style, is correct.

    Think: In a sentence, a comma symbolizes a pause; there will be more to com in the thought shortly. A period symbolizes the end of a thought and the start of another. Here's an example number: 123,456.789 … That's One-hundred-twenty-three-thousand four-hundred-fifty-six and seven-hundred-eighty-nine thousandths.

    The integer part of the number is 123456. The comma between the sets of three is a pause; more of the “thought” will follow after the 123. To put it another way, more of the integer part of the number will be read shortly.

    Imagine if a period was used: The end of the “thought” would come at the period in regular writing. The thing is, when a period is used, you split the integer part of the number in half. Why should the “thought” end halfway through one part of the number? The “thought” should continue until the integer part of the number is finished.

    Anyway, that's why I think the period should symbolize the start of the decimal part of a number rather than a comma. We should keep math and writing consistent, don't you think?

    I hate defending the United States of America in this way, but I think we're right for once. … Hey, I live here and I think most of the people are culturally illiterate buffoons, but I'll cut us some slack this time. We're right, anyone who thinks differently is wrong.

  • Aaron

    Well, I have thought long and hard about this particular issue — the one with commas versus periods. I have come to the conclusion that the United States, and any other country that uses the comma style, is correct.

    Think: In a sentence, a comma symbolizes a pause; there will be more to com in the thought shortly. A period symbolizes the end of a thought and the start of another. Here's an example number: 123,456.789 … That's One-hundred-twenty-three-thousand four-hundred-fifty-six and seven-hundred-eighty-nine thousandths.

    The integer part of the number is 123456. The comma between the sets of three is a pause; more of the “thought” will follow after the 123. To put it another way, more of the integer part of the number will be read shortly.

    Imagine if a period was used: The end of the “thought” would come at the period in regular writing. The thing is, when a period is used, you split the integer part of the number in half. Why should the “thought” end halfway through one part of the number? The “thought” should continue until the integer part of the number is finished.

    Anyway, that's why I think the period should symbolize the start of the decimal part of a number rather than a comma. We should keep math and writing consistent, don't you think?

    I hate defending the United States of America in this way, but I think we're right for once. … Hey, I live here and I think most of the people are culturally illiterate buffoons, but I'll cut us some slack this time. We're right, anyone who thinks differently is wrong.

  • xiangxiang
  • test

    How do I force the number formatter to display 1.25m instead of 1,250,000, etc? Thanks

blog comments powered by Disqus