Javascript Number toLocaleString()

Introduction

The Javascript Number toLocaleString() method returns a string representation of this number based on current locale.

numObj.toLocaleString([locales [, options]])
Parameter Optional Meaning
locales Yes Locales for settings
options Yesoptions to use

Using toLocaleString()

var number = 3500;
console.log(number.toLocaleString());

Using locales

var number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));

Using options

The results provided by toLocaleString() can be customized using the options argument:

var number = 123456.789;

// request a currency format
console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));

// the Japanese yen doesn't use a minor unit
console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))

// limit to three significant digits
console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));

// Use the host default language with options for number formatting
var num = 30000.65;
console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));



PreviousNext

Related