Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 396
Atul Bhatia Avatar asked Mar 10 '14 13:03

Atul Bhatia


3 Answers

Change the number of fraction digits based upon whether or not the number is whole.

- (NSString *)stringFromNumber:(NSNumber *)number
{
    BOOL isWholeNumber = (roundf(number.doubleValue) == number.doubleValue);
    self.currencyNumberFormatter.maximumFractionDigits = self.currencyNumberFormatter.minimumFractionDigits = isWholeNumber ? 0 : 2;

    NSString *str = [self.currencyNumberFormatter stringFromNumber:number];
    return str;
}
like image 92
jowie Avatar answered Nov 04 '22 10:11

jowie


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.

like image 40
Austin Avatar answered Nov 04 '22 09:11

Austin


I am using the following solution in Swift. It is based on jowie's answer except I do not want to change maximumFractionDigits if my number is not whole. In some countries more than 2 digits are used for prices.

if(price==price.decimalNumberByRoundingAccordingToBehavior(nil))
{
  numberFormatter.maximumFractionDigits=0
  numberFormatter.minimumFractionDigits=0
}
let priceStr = numberFormatter.stringFromNumber(price)!
like image 23
Dmitry Avatar answered Nov 04 '22 08:11

Dmitry