Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tombuarts

macrumors newbie
Original poster
Jan 30, 2010
15
0
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.
 
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!
 
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;
}
 
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.
 
Check out NSNumberFormatter.
1. You'll need to develop the skill of searching the documentation

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.
 
Exactly the kind of un-useful, un-helpful answer I've come to expect out of 'beards on the Internet.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.