Yogesh Chauhan's Blog

How to implement NSNumberFormatter in Swift for currency?

in Swift on March 5, 2020

To show a number in proper currency format, we are going to use following classes.

NSNumber

An object wrapper for primitive scalar numeric values.

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. It also defines a compare(_:) method to determine the ordering of two NSNumber objects.

Declaration


class NSNumber : NSValue

Read more on Apple's Official Guide: https://developer.apple.com/documentation/foundation/nsnumber

NumberFormatter

A formatter that converts between numeric values and their textual representations.

Declaration


class NumberFormatter : Formatter

Instances of NumberFormatter format the textual representation of cells that contain NSNumber objects and convert textual representations of numeric values into NSNumber objects. The representation encompasses integers, floats, and doubles; floats and doubles can be formatted to a specified decimal position. NumberFormatter objects can also impose ranges on the numeric values cells can accept.

Read more on Apple's Official Guide: https://developer.apple.com/documentation/foundation/numberformatter

How to use those claases to print currency symbol?

Let's look at this example:


let price = 12.34 as NSNumber
let formatter = NumberFormatter()

formatter.numberStyle = .currency
print(formatter.string(from: price)!)

The primary purpose of the NSNumber class is to allow the storage of primitive numerical data in the form of an object. The NSNumber class is able to store signed and unsigned char, short, integer, int, long and long long data types and also float, double and Bool values.

Also, When we display a number in Swift in Float, Double, or Int, it will display the numbers without grouping separators. So, by default 9,992,098.0 will be displayed as 9992098.0. S, we need to use the NumberFormatter (NSNumberFormatter in older Swift) class to convert your number into a formatted String for a text field.

To display currency, we will need to show the currency symbol ($, €, ¥, £) for the current locale and for that we use NSNumberFormatter, which is now NumberFormatter class.

Finally, formatter.numberStyle = .currency converts the styles into currency style. We can use .numberStyle to show decimal, scientific and many more styles as well but all we need is currency style right now.

Now, the example above is going to display the default currency format (USD).


//Output

$12.34

If we want to display currency in whole number, we can use this:


formatter.locale = Locale(identifier: "es_CL")
print(formatter.string(from: price)!)

//Output

$12

We can also define other currency symbol. For example, let's display Indian Rupee symbol.


formatter.locale = Locale(identifier: "brx_IN")
print(formatter.string(from: price)!)

//Output

₹ 12.34

We can actually display the whole currency in Indian language:


formatter.locale = Locale(identifier: "bn_IN")
print(formatter.string(from: price)!)

//Output

১২.৩৪₹

Here is the full list of iOS Locale Identifiers on Github: https://gist.github.com/st3fan/18f95c9c51de4e8df0302aac8eaa58ae


Most Read

#1 How to add Read More Read Less Button using JavaScript? #2 How to check if radio button is checked or not using JavaScript? #3 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Block IPs and User Agents using code in Drupal or WordPress?DrupalHow to Write Complex Search Queries in SQL?SQL/MySQLWhat is ECMAScript?JavaScriptHow to Access a Global Variable From Inside a Function in PHP?PHPOpen Source Security Tools for Defense SecurityMiscellaneousClearwater Seafoods – B2C in ChinaMiscellaneous