revision:
The toLocaleString() method returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString methods and these Strings are separated by a locale-specific String (such as a comma ",").
toLocaleString() toLocaleString(locales) toLocaleString(locales, options)
Parameters:
locales : optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument,
options : optional. An object with configuration properties
const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')]; const localeString = array1.toLocaleString('en', { timeZone: 'UTC' }); console.log(localeString); // Expected output: "1,a,12/21/1997, 2:12:00 PM", // This assumes "en" locale and UTC timezone - your results may vary function eArabic(x){ return x.toLocaleString('ar-EG'); } console.log(eArabic(123456.789)); // Expected output: "١٢٣٬٤٥٦٫٧٨٩" console.log(eArabic('123456.789')); // Expected output: "123456.789" console.log(eArabic(NaN)); // Expected output: "ليس رقم"
The toLocaleString() method returns a string representing the object. This method is meant to be overridden by derived objects for locale-specific purposes.
toLocaleString()
Parameters: none
const date1 = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); console.log(date1.toLocaleString('ar-EG')); // Expected output: "٢٠/١٢/٢٠١٢ ٤:٠٠:٠٠ ص" const number1 = 123456.789; console.log(number1.toLocaleString('de-DE')); // Expected output: "123.456,789"
<div> <p id="local-1"></p> <p id="local-2"></p> </div> <script> const prices = ["¥7", 500, 8123, 12]; prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }); // "¥7,¥500,¥8,123,¥12" document.getElementById("local-1").innerHTML = ["¥7", 500, 8123, 12].toLocaleString("ja-JP", { style: "currency", currency: "JPY" }); document.getElementById("local-2").innerHTML = prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }); </script>
<div> <p id="local-3"></p> <p id="local-4"></p> <p id="local-5"></p> <p id="local-6"></p> <p id="local-7"></p> <p id="local-8"></p> <p id="local-9"></p> <p id="local-10"></p> <p id="local-11"></p> <p id="local-12"></p> <p id="local-13"></p> </div> <script> const number = 123456.789; document.getElementById("local-3").innerHTML = "number : " + number // German uses comma as decimal separator and period for thousands console.log(number.toLocaleString("de-DE")); // → 123.456,789 document.getElementById("local-4").innerHTML = "Germany : " + number.toLocaleString("de-DE"); // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(number.toLocaleString("ar-EG")); // → ١٢٣٤٥٦٫٧٨٩ document.getElementById("local-5").innerHTML = "Arabic : " + number.toLocaleString("ar-EG"); // India uses thousands/lakh/crore separators console.log(number.toLocaleString("en-IN")); // → 1,23,456.789 document.getElementById("local-6").innerHTML = "India : " + number.toLocaleString("en-IN"); // the nu extension key requests a numbering system, e.g. Chinese decimal console.log(number.toLocaleString("zh-Hans-CN-u-nu-hanidec")); // → 一二三,四五六.七八九 document.getElementById("local-7").innerHTML = "China : " + number.toLocaleString("zh-Hans-CN-u-nu-hanidec"); // when requesting a language that may not be supported, such as Balinese, include a fallback language, in this case Indonesian console.log(number.toLocaleString(["ban", "id"])); // → 123.456,789 document.getElementById("local-8").innerHTML = "Indonesia - Bali : " + number.toLocaleString("ban", "id"); // request a currency format console.log(number.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),); // → 123.456,79 € document.getElementById("local-9").innerHTML = "Germany - formatted: " + (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" }),); // → ¥123,457 document.getElementById("local-10").innerHTML = "Japan - formatted: " + (number.toLocaleString("ja-JP", { style: "currency", currency: "JPY" })); // limit to three significant digits console.log(number.toLocaleString("en-IN", { maximumSignificantDigits: 3 })); // → 1,23,000 document.getElementById("local-11").innerHTML = "India - formatted: " + (number.toLocaleString("en-IN", { maximumSignificantDigits: 3 })); // Use the host default language with options for number formatting const num = 30000.65; console.log( num.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, }), ); document.getElementById("local-12").innerHTML = "host default language " + num.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2,}); </script>
<div> <p id="local-14"></p> <p id="local-15"></p> <p id="local-16"></p> <p id="local-17"></p> </div> <script> const obj = { toString() { return "My Object"; }, }; console.log(obj.toLocaleString()); // "My Object" document.getElementById("local-14").innerHTML = "toLocaleString() : " + obj.toLocaleString(); const number1 = 123456.789; // convert number to string with a language-sensitive representation for the Indian English locale const numberString = number.toLocaleString('en-IN'); console.log(numberString); document.getElementById("local-15").innerHTML = "number to local settings : " + numberString; // Output: 1,23,456.789 // create a new date object const date = new Date(Date.now()); console.log(date); document.getElementById("local-16").innerHTML = "date : " + date; // convert the date using German locale formatting convention let dateStr = date.toLocaleString("de"); console.log(dateStr); document.getElementById("local-17").innerHTML = "date to German locale settings : " + dateStr; </script>