ioscurrencynsnumberformatter

ios numberformatter currency remove trailing decimals if 0


I want to format my number into a currency string. These are the following cases

25.00 => $25
25.43 => $25.43
25.4 => $25.40
0.00 -> $0

Is there a way to do this in NSNumberFormatter?

This is my code right now:

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setNumberStyle:NSNumberFormatterCurrencyStyle];
[fmt setCurrencyCode:@"USD"];

However that fails for my first and last examples.

I also tried:

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setPositiveFormat:@"$0.##"];

However that fails for my third case. Any suggestions?


Solution

  • I don't think there's a way to do this using a plain NSNumberFormatter. You could set the minimum and maximum fraction digits to 0 just for formatting integers in a subclass of NSNumberFormatter:

    @interface MyCurrencyFormatter : NSNumberFormatter
    @end
    
    @implementation MyCurrencyFormatter
    
    - (id)init {
        if ((self = [super init])) {
            [self setNumberStyle:NSNumberFormatterCurrencyStyle]];
            [self setCurrencyCode:@"USD"];
        }
        return self;
    }
    
    - (NSString *)stringFromNumber:(NSNumber *)aNumber {
        NSInteger minimumFractionDigits = [self minimumFractionDigits];
        NSInteger maximumFractionDigits = [self maximumFractionDigits];
        if ([self isInteger:aNumber]) {
            [self setMinimumFractionDigits:0];
            [self setMaximumFractionDigits:0];
        }
        NSString *formattedNumber = [super stringFromNumber:aNumber];
        [self setMinimumFractionDigits:minimumFractionDigits];
        [self setMaximumFractionDigits:maximumFractionDigits];
        return formattedNumber;
    }
    
    - (BOOL)isInteger:(NSNumber *)aNumber {
        NSDecimal decimalValue = aNumber.decimalValue;
        NSDecimalRound(&decimalValue, &decimalValue, 0, NSRoundDown);
        NSDecimalNumber *roundedValue = [[NSDecimalNumber alloc] initWithDecimal:decimalValue]
        return [aNumber isEqualToNumber:roundedValue];    
    }
    
    @end
    

    This should handle international number formats as well.

    Credit to this post for determining if a number is an integer.