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?
is only way to trancate decimal digits and decimal separator in a NSNumberFormatterCurrencyStyle formatter.
result
12,345 $
You will need to use a
NSNumberFormatter
which supports currency.Prints: $10,395,209.00
You should definitely use
NSNumberFormatter
for this. The basic steps are: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.
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:
Don't forget to clean up!
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:
(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.)