Objective-C: How to format string as $ Price

Mustafa picture Mustafa · Nov 21, 2009 · Viewed 22.5k times · Source

Is their a built-in way of formatting string as $ price, e.g. 12345.45 converted to $12,345.45?

Answer

Rhult picture Rhult · Nov 21, 2009

Assuming you are using Cocoa (or just Foundation), you can use NSNumberFormatter and set its style to currency:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
... = [formatter stringFromNumber:number];

By default it uses the locale of your system, but you can change that and lots of other properties, see the NSNumberFormatter API docs.