Javascript Intl.NumberFormat

Introduction

The Javascript Intl.NumberFormat does language sensitive number formatting.

Without specifying a locale.

var number = 3500;

console.log(new Intl.NumberFormat().format(number));

Using locales

var number = 123456.789;
console.log(new Intl.NumberFormat('de-DE').format(number));
console.log(new Intl.NumberFormat('ar-EG').format(number));
console.log(new Intl.NumberFormat('en-IN').format(number));

Using options

The results can be customized using the options argument:

var number = 123456.789;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));

Using style and unit

console.log(new Intl.NumberFormat("pt-PT",   {
        style: 'unit',
         unit: "mile-per-hour"
}).format(50));/*from ww w  .j a v a 2  s. c  om*/

console.log((16).toLocaleString('en-GB', {
         style: "unit",
         unit: "liter",
         unitDisplay: "long"
}));

Using notation

console.log(new Intl.NumberFormat('en-US', { notation: "scientific" }).format(987654321));
console.log(new Intl.NumberFormat('pt-PT', { notation: "scientific" }).format(987654321));
console.log(new Intl.NumberFormat('en-GB', { notation: "engineering" }).format(987654321));
console.log(new Intl.NumberFormat('de', { notation: "engineering" }).format(987654321));
console.log(new Intl.NumberFormat('zh-CN', { notation: "compact" }).format(987654321));
console.log(new Intl.NumberFormat('fr', { notation: "compact" , compactDisplay: "long" }).format(987654321));
console.log(new Intl.NumberFormat('en-GB', { notation: "compact" , compactDisplay: "short" }).format(987654321));



PreviousNext

Related