Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 306
Satyam Avatar asked Mar 08 '12 09:03

Satyam


2 Answers

Use the maximumFractionDigits setting:

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

You can adjust the value accordingly. Using 2 will limit the result to two digits, for this question a value of 0 should do it.

like image 111
drewish Avatar answered Oct 01 '22 04:10

drewish


Here is solution in swift 3

    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    //formatter.locale = NSLocale.current
    formatter.currencySymbol = "" //In case you do not want any currency symbols
    formatter.maximumFractionDigits = 0

Setting the maximumFractionDigits to 0 will not show any decimal or fractional values.

like image 16
Zulqarnain Mustafa Avatar answered Oct 01 '22 05:10

Zulqarnain Mustafa