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?
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;
}
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.
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)!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With