The NSNumberFormatter multiplier is rounded down, not showing a fraction

I configured NSNumberFormatter to convert amounts that are stored as cents to NSDictionary for euros. Since they are stored as cents, I set the multiplier formatter to [NSNumber numberWithDouble:0.01] . However, when I try to display 304 euro cents in euros, I get € 3,00 .

This makes me think that the multiplier performs integer division instead of double division.

NSFormatter Configuration

 /** Returns an NSNumberFormatter that can be used to display currency in euro (as determined in The Netherlands). */ + (NSNumberFormatter *)euroCurrencyFormatter { static NSNumberFormatter *numberFormatter = nil; @synchronized(self) { if (!numberFormatter) { numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [numberFormatter setMultiplier:[NSNumber numberWithDouble:0.01]]; NSLocale *nlLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"]; [numberFormatter setLocale:nlLocale]; [nlLocale release]; } } return numberFormatter; } 

Call NSFormatter

 cell.detailTextLabel.text = [[NSNumberFormatter euroCurrencyFormatter] stringFromNumber:[breakdown valueForKey:@"VatAmount"]]; // The VAT amount would be 304. 

Result

€ 3,00

How to stop jerky behavior?

+4
source share
2 answers

Try this code.

 cell.detailTextLabel.text = [[NSNumberFormatter euroCurrencyFormatter] stringFromNumber:[NSNumber numberWithDouble:[[breakdown valueForKey:@"VatAmount"] doubleValue]]]; // The VAT amount would be 304. 

As if you are transferring VAT as an integer, you will always receive the number in int format.

+3
source

See "Customizing Rounding Behavior" NSNumberFormatter Link .

0
source

Source: https://habr.com/ru/post/1392455/


All Articles