Formatting a number to show commas and/or dollar sign

后端 未结 3 1961
攒了一身酷
攒了一身酷 2020-11-30 11:38

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 *lab         


        
相关标签:
3条回答
  • 2020-11-30 11:47

    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.)

    0 讨论(0)
  • 2020-11-30 11:58
    [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 $

    0 讨论(0)
  • 2020-11-30 12:05

    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

    0 讨论(0)
提交回复
热议问题