ios numberformatter currency remove trailing decimals if 0
Asked Answered
B

4

12

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?

Bernetta answered 10/3, 2014 at 13:56 Comment(4)
You could simply use a standard formatter then see if the result ends with .00. If so, strip it off.Ethelinda
Yeah that makes sense, I'll do that. Thanks!Bernetta
BTW - are you sure you want to hardcode the currency? And what about dealing with decimal formats for other locales? Depending on the user's locale you might get 25,00$ or various other formats. Keep that in mind.Ethelinda
For now I am only dealing with the USBernetta
B
6

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.

Bookmaker answered 10/3, 2014 at 14:0 Comment(6)
This won't work for $25 or any other value with zero cents.Ethelinda
yes @Ethelinda is correct, it fails for my first case.Bernetta
Sorry, misunderstood what you were trying to doBookmaker
Thanks austin, exactly what i was thinking.Bernetta
Please, do not use this solution for international apps. Some locales use the following format 1.000.000,00 and you certainly do not want to remove those .00Ermin
@Ermin Thanks for the feedback. I updated my answer to a more elegant, internationalization-friendly version.Bookmaker
L
8

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;
}
Lamp answered 8/5, 2014 at 10:22 Comment(0)
B
6

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.

Bookmaker answered 10/3, 2014 at 14:0 Comment(6)
This won't work for $25 or any other value with zero cents.Ethelinda
yes @Ethelinda is correct, it fails for my first case.Bernetta
Sorry, misunderstood what you were trying to doBookmaker
Thanks austin, exactly what i was thinking.Bernetta
Please, do not use this solution for international apps. Some locales use the following format 1.000.000,00 and you certainly do not want to remove those .00Ermin
@Ermin Thanks for the feedback. I updated my answer to a more elegant, internationalization-friendly version.Bookmaker
E
6

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)!
Ermin answered 22/10, 2015 at 7:37 Comment(0)
T
0

A "swifty" way to achieve the desired result, but remain flexible is to set a range by setting the minimum and maximum fraction digits to show:

let price: NSDecimalNumber // 299.0
let priceLocale: Locale    // .current

let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 0

let result = formatter.string(from: price) ?? "" // 299 $
Tenant answered 21/10, 2021 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.