HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
toLocaleString() は Object インスタンスのメソッドで、オブジェクトを表す文字列を返します。このメソッドは、ロケール固有の目的のために派生オブジェクトによってオーバーライドするためのものです。
toLocaleString()
Object
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); console.log(date.toLocaleString("ar-EG")); // 予想される結果: "٢٠/١٢/٢٠١٢ ٤:٠٠:٠٠ ص" const number = 123456.789; console.log(number.toLocaleString("de-DE")); // 予想される結果: "123.456,789"
なし。ただし、このメソッドをオーバーライドするすべてのオブジェクトは、 Number.prototype.toLocaleString のように locales と options に対応する最大 2 つの引数を受け取ることが期待されます。これらの引数の位置は、他の目的には使用しないでください。
Number.prototype.toLocaleString
locales
options
this.toString() 呼び出しの返値です。
this.toString()
Object.prototype 継承するすべてのオブジェクト(つまり、 null プロトタイプオブジェクトを除くすべてのオブジェクト)は、 toLocaleString() メソッドを継承します。 Object's toLocaleString は、 this.toString() を呼び出した結果を返します。
Object.prototype
null
toLocaleString
この関数は、オブジェクトに汎用的な toLocaleString メソッドを提供するために用意されています。コア言語では、これらの組み込みオブジェクトは toLocaleString をオーバーライドしてロケール特有の書式設定を行います。
Array
Array.prototype.toLocaleString()
Number
Number.prototype.toLocaleString()
Date
Date.prototype.toLocaleString()
TypedArray
TypedArray.prototype.toLocaleString()
BigInt
BigInt.prototype.toLocaleString()
基底の toLocaleString() メソッドは、単純に toString() を呼び出します。
toString()
const obj = { toString() { return "My Object"; }, }; console.log(obj.toLocaleString()); // "My Object"
Array.prototype.toLocaleString()は、各要素の toLocaleString() メソッドを呼び出して、結果をロケール特有の区切り文字で連結することで、配列の値を文字列として出力するために使用されます。例を示します。
const testArray = [4, 7, 10]; const euroPrices = testArray.toLocaleString("fr", { style: "currency", currency: "EUR", }); // "4,00 €,7,00 €,10,00 €"
Date.prototype.toLocaleString() は、特定のロケールに適した日付表示を出力するために使用されます。例を示します。
const testDate = new Date(); // "Date Fri May 29 2020 18:04:24 GMT+0100 (イギリス夏時間)" const deDate = testDate.toLocaleString("de"); // "29.5.2020, 18:04:24" const frDate = testDate.toLocaleString("fr"); // "29/05/2020, 18:04:24"
Number.prototype.toLocaleString() は、特定のロケールに適した数値表示を出力するために使用されます。例を示します。
const testNumber = 2901234564; // "2901234564" const deNumber = testNumber.toLocaleString("de"); // "2.901.234.564" const frNumber = testNumber.toLocaleString("fr"); // "2 901 234 564"
Enable JavaScript to view this browser compatibility table.
Object.prototype.toString()