I have an NSNumberFormatter
which I'm trying to use to generate a whole number of GBP (£) from an NSNumber
. I keep getting two decimal places regardless of which incantation I try. My code is:
NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
[fmtCurrency setGeneratesDecimalNumbers:FALSE];
[fmtCurrency setCurrencyCode:@"GBP"];
[fmtCurrency setCurrencySymbol:@"£"];
txtTotal.text = [fmtCurrency stringFromNumber: result.Bill ]; // this is an NSNumber
I think you want to call setMaximumFractionDigits with zero.
NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
[fmtCurrency setGeneratesDecimalNumbers:NO];
[fmtCurrency setCurrencyCode:@"GBP"];
[fmtCurrency setCurrencySymbol:@"£"];
NSNumber* myNumber = [NSNumber numberWithFloat:12.34];
NSString* myString = [fmtCurrency stringFromNumber:myNumber];
NSLog( @"the number is %@", myString );
[fmtCurrency setMaximumFractionDigits:0];
myString = [fmtCurrency stringFromNumber:myNumber];
NSLog( @"now it's %@", myString );
2011-07-25 01:05:39.823 FormatterTest[841:707] the number is £12.34
2011-07-25 01:05:39.824 FormatterTest[841:707] now it's £12
You can try the following
[fmtCurrency setGeneratesDecimalNumbers:NO];
[fmtCurrency setMaximumFractionDigits:0];
or
Get intValue
from the NSNumber
as suggested by @Praveen
Swift 4
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 0
You need to extract the intValue from NSNumber.
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