How to add commas to numbers?

Discussion in 'iOS Programming' started by tombuarts, May 14, 2010.

  1. tombuarts macrumors newbie

    Joined:
    Jan 30, 2010
    #1
    How do I make 2134547 display as 2,134,547 in Objective C? Is there a simple way to do this? Any help would be greatly appreciated.
     
  2. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #2
    Check out NSNumberFormatter.

    A few points:
    1. You'll need to develop the skill of searching the documentation, if you're not used to it now is a good time to start. In Xcode, select Help from the menu, then Developer Documentation. Then type NSNumberFormatter in the search bar.

    2. It's not Objective-C that does this, it's Cocoa. You want to make sure you understand the difference (Objective-C is the language, Cocoa is the set of objects that Apple gives us).

    Good luck!
     
  3. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #3
    Converting Numbers to Strings With Thousands Separator

    Here is an example of code that converts numbers to strings with thousands separators:

    Code:
    NSNumberFormatter* createCurrencyFormatter() {
    	NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    	[formatter setNumberStyle: kCFNumberFormatterCurrencyStyle];
    	[formatter setCurrencySymbol:  @""];
    	return formatter;
    }
    
    NSString* doubleToCurrencyString(double d) {
    	
    	// convert the double to an NSNumber
    	NSNumber *number = [NSNumber numberWithDouble: d];
    	
    	// create a number formatter object
    	NSNumberFormatter *formatter = createCurrencyFormatter();	
    	
    	// convert the number to a string
    	NSString *string = [formatter stringFromNumber: number];
    	
    	// release just the formatter (the number and the string will be release in the autorelease pool)
    	[formatter release];
    	
    	// return the string
    	return string;
    }
    
    double currencyStringToDouble(NSString *string) {
    	
    	// if the string is nil, return zero
    	if (nil == string) 
    		return 0.0;
    	
    	// create a number formatter object
    	NSNumberFormatter *formatter = createCurrencyFormatter();
    	
    	// convert the double to an NSNumber
    	NSNumber *number = [formatter numberFromString: string];
    	double d = [number doubleValue];
    	
    	// release just the formatter (the number will be released in the autorelease pool)
    	[formatter release];
    	
    	// return the string
    	return d;
    }
    
     
  4. tombuarts thread starter macrumors newbie

    Joined:
    Jan 30, 2010
    #4
    Success, success!!! I got it to work using setGroupingSeparator and setGroupingSize. I just had some of the coding off a little bit, but got it to work with the example posted. Thanks for the help.
     
  5. BobDiddle macrumors newbie

    Joined:
    May 19, 2010
    #5
    Exactly the kind of un-useful, un-helpful answer I've come to expect out of 'beards on the Internet. I'm glad someone else answered his question, at least.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Frequently, the skill to search the documentation is entirely useful and entirely helpful. You can often find the answer to a question much faster than posting a thread and waiting for a response (that may never come). Also, it helps hone your skills for taking basic information and using your creativity and imagination to transform it into working code versus copy-and-paste coding, which isn't real programming anyways.
     

Share This Page