👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
In JavaScript, the Intl.NumberFormat () method allows you to specify a currency that you want to represent by a number. The method is a constructor that can be used to format the currency in its style property.
This article explains how we use Intl.NumberFormat () to achieve this, along with some sample code.
Decomposition of the constructor and its parameters
Since Intl.NumberFormat () is a constructor, it will be preceded by the "new" JavaScript keyword to create a object capable of obtaining specialized digital formatting.
The constructor accepts two optional parameters, "locales" and "options". The locale will contain a string with Current Internet Best Practices (BCP) for language tags or, currently, BCP 47. For example, if we wanted US English, we would use the BCP 47 language tag of " en-US ", if we wanted Brazilian Portuguese, we would use" pt-BR ".
Options has 17 different properties that it can contain, but in this article we will only focus on three. We also focus on the style with which we can use Currency, CurrencyDisplay and CurrencySign.
Sample code
Some things to note in our code example above are that we used the format ()
method to pass our variable silver. We did this after creating the new object, which is used to format our currency according to our preferences, which are passed as parameters.