21

Is it possible to display 1000.99 as 1,000.99 using

[NSString stringWithFormat:@"£%0.2f", 1000.99]

Please guide me if I am not on the right track to achieve this?

brainjam
  • 18,863
  • 8
  • 57
  • 82
Leo
  • 1,547
  • 3
  • 24
  • 40

2 Answers2

36

Using NSNumberFormatter:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat: 1000.99]];
NSLog(@"%@", numberAsString);
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Chris Gummer
  • 4,772
  • 1
  • 24
  • 17
21

See NSNumberFormatter.

It can handle all your numeric formatting needs and do so in an automatically localized fashion, when used correctly (since currencies around the world are often written with different punctuation & formatting).

In particular, check out the number formatting guide linked to at the top of the class documentation

brainjam
  • 18,863
  • 8
  • 57
  • 82
bbum
  • 162,346
  • 23
  • 271
  • 359
  • Can you please provide with a simple example? Thanks – Leo Dec 05 '09 at 20:15
  • @bbum: here is the iPhone specific documentation http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html – user7116 Dec 05 '09 at 20:17
  • 1
    +1 `NSNumberFormatter` is definitely the correct way to do this. – Dave DeLong Dec 05 '09 at 22:57
  • Thanks guys. NSNumberFormatter is deninitely the right answer. I have managed to figure out my solution from documentation before Chris Gummer answer (His answer is exactly the way you should do it) that's why I am marking bbum's asnwer. Once again thank you all and hope it would save some ones time. – Leo Dec 08 '09 at 08:56