14

I want to format my UILabel with commas or better with a dollar sign and commas (with no decimal).

Here is the code I am using:

IBOutlet UILabel *labelrev

float rev = (x + y)

labelrev.text = [[NSString alloc] initWithFormat:@%2.f",rev];

I get xxxxxxxxx as the output I want to get xxx,xxx,xxx or $xxx,xxx,xxx

How do I do that?

Moshe
  • 57,511
  • 78
  • 272
  • 425
Peter B
  • 169
  • 1
  • 1
  • 6
  • 1
    Welcome to StackOverflow! You should probably choose a username (instead of user668830). – Moshe Mar 23 '11 at 15:00
  • 2
    Great! Remember to encourage others to help you by selecting the answer that is correct. – Moshe Mar 23 '11 at 17:58

3 Answers3

44

You should definitely use NSNumberFormatter for this. The basic steps are:

  1. Allocate, initialize and configure your number formatter.
  2. Use the formatter to return a formatted string from a number. (It takes an NSNumber, so you'll need to convert your double or whatever primitive you have to NSNumber.)
  3. Clean up. (You know, memory management.)

This code sets up the number formatter. I've done everything that you want except the currency bit. You can look that up in the documentation.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];

Next, you want to set up your number and return a formatted string. In your case, we wrap a double in an NSNumber. I do it inline, but you can break it up into two steps:

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];

Don't forget to clean up!

[formatter release];

A quick note about localization:

The NSLocale class provides some useful info about the user's locale. In the first step, notice how I used NSLocale to get a localized grouping separator:

NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

(Some countries use a full-stop/period, while others use a comma.) I think there's a way to get a localized currency symbol as well, but I'm not one hundred percent sure, so check the documentation. (It depends upon what your trying to do.)

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • @PeterB - Glad to hear that it worked. What was the issue with long code and how did you resolve it? I don't understand that part of what you're saying. – Moshe Mar 23 '11 at 21:25
  • Thanks Moshe it works great....I am working on an iphone app and I have limited space for the $xxx,xxx.xx format on the screen. So I took out the currency format line in the code. I put the $ on the UI instead of in the NSNumber format and now I get xxx,xxx on the UILabel which looks better on my screen layout. – Peter B Mar 24 '11 at 00:42
  • Moshe how do you use this and leave off the decimal? so it just prints the whole numerber $xx,xxx..thanks – Peter B Mar 27 '11 at 13:38
  • @Moshe, thanks for the great and detailed answer (with not referring to the Apples documentations...). I've really some difficulties with creating the right format for the number output. One question though. What does it take to ensure the local decimal separator for the `NSNumbers` as string in `UILabels`? Thanks again! – JFS May 30 '13 at 12:47
  • @Moshe unfortunately, getting the grouping separator from the locale is not enough to make this correctly localized. For example, in India, where lakh and crore are used much more commonly than thousands and millions, currency should be formatted like ₹2,00,00,000 -- note that the grouping size is not uniform. This is 2 crore rupees, or 20 million in US-style numbers. – Brennan Vincent Apr 26 '16 at 20:45
13

You will need to use a NSNumberFormatter which supports currency.

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", [currencyFormatter stringFromNumber:[NSNumber numberWithInt:10395209]]);
[currencyFormatter release];

Prints: $10,395,209.00

Joe
  • 56,979
  • 9
  • 128
  • 135
2
[formatterCurrency setMaximumFractionDigits:0]

is only way to trancate decimal digits and decimal separator in a NSNumberFormatterCurrencyStyle formatter.

NSNumberFormatter *formatterCurrency;
formatterCurrency = [[NSNumberFormatter alloc] init];

formatterCurrency.numberStyle = NSNumberFormatterCurrencyStyle;
[formatterCurrency setMaximumFractionDigits:0];
[formatterCurrency stringFromNumber: @(12345.2324565)];

result

12,345 $

İzzet Okbay
  • 61
  • 1
  • 3