Search code examples
iosnsnumberformatter

iOS - Showing currency without cents using NSNumberFormatter


I'm able to convert a particular NSNumber into currency using NSNUmberFormatter. Its showign $ symbol, numbers separated by "," etc.
But I don't want to display cents.
For example its displaying $537,335.32
I just want to display $536,335
I'm writing the following code:

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSDictionary* tempDict = [self.displayDict objectForKey:kConservative];
conservativeHP.text = [currencyFormatter stringFromNumber:[tempDict objectForKey:kHomePrice]];

How should I create NSNumberFormatter or do something so that the result won't have "decimals"


Solution

  • Before adding the number to the formatter, change it to int to avoid the decimals, something like this:

    float myValue = 543.32;
    
    NSNumber myNumber = [NSNumber numberWithInt: (int)myValue)];
    

    You will get rid of the decimal part!