JavaScript Intl.NumberFormat() Constructor
Last Updated :
06 Jun, 2023
The Javascript Intl.NumberFormat() constructor is used to create Intl.NumberFormat() object that enables language-sensitive number formatting. This constructor is created with or without a new keyword both create a new Intl.NumberFormat instance.
Syntax:
new Intl.NumberFormat()
new Intl.NumberFormat([locales[, options]])
Parameters:
- Locales: It contains a String or an array of Strings that allow the following value.
- num: It specifies the numbering system to be used which can be arab, bali, latn, tibt etc.
- Options: It is an object which can have properties like compactDisplay, currency,currencyDisplay, currencySign ,style, unit,unitDisplay, etc.
Return value: It return a new Intl.NumberFormat object.
Example 1: In this example, we will use the basic use of Intl.NumberFormat() constructor.
Javascript
let amount = 10000;
let newAmount = new Intl.NumberFormat().format(amount)
console.log(newAmount);
|
Output:
10,000
Example 2: In this example, we will perform Decimal and percentage formatting with the help of Intl.NumberFormat() constructor.
Javascript
let amount = 2000;
let newAmount1 = new Intl.NumberFormat( 'en-US' ,
{ style: 'decimal' }).format(amount);
let newAmount2 = new Intl.NumberFormat( 'en-US' ,
{ style: 'percent' }).format(amount);
console.log(newAmount1);
console.log(newAmount2);
|
Output:
2,000
200,000%
Example 3: In this example, we will perform unit formatting and currency formatting with the help of Intl.NumberFormat() constructor.
Javascript
let num = 5000;
let num1 = new Intl.NumberFormat( 'en-US' ,
{ style: 'unit' , unit: 'liter' }).format(num);
let num2 = new Intl.NumberFormat( 'en-US' ,
{
style: 'unit' ,
unit: 'liter' ,
unitDisplay: 'long'
}).format(num);
let currancy = 50;
let newCurrancy = new Intl.NumberFormat( 'en-US' ,
{
style: 'currency' ,
currency: 'USD'
}).format(currancy);
console.log(num1);
console.log(num2);
console.log(newCurrancy);
|
Output:
5,000 L
5,000 liters
$50.00
Supported Browsers:
- Chrome 24
- Edge 12
- Firefox 29
- Opera 15
- Safari 10
We have a complete list of JavaScript Intl methods to check please go through, the JavaScript Intl Reference article.
Share your thoughts in the comments
Please Login to comment...