tags:

views:

1355

answers:

2

I have a NSString value of @"78000". How do I get this in currency format, i.e. $78,000 with it remaining an NSString.

+9  A: 

You need to use a number formatter. Note this is also how you would display dates/times etc in the correct format for the users locale

// alloc formatter
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];

// set options.
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];

NSNumber *amount = [NSNumber numberWithInteger:78000];

// get formatted string
NSString* formatted = [currencyStyle stringFromNumber:amount]

[currencyStyle release];
Andrew Grant
A: 

cool. The result is "$78,000.00"

How do you format without the decimal/cents?

I can remove the decimal itself, and divide by 100 to get the value to display properly: [currencyStyle setCurrencyDecimalSeparator:@"."]; ... But, at 0, the result will be "$000"

What I've done for now is split the result at "." and show just the dollars. Splitting seems like a bit too much. I could use some help with finding a better way to filter out the decimal and cents, or get the formatter to omit it.

funrob
You should post that as a separate question, not as an answer on this question, since it does not answer this question.
Peter Hosey