pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString

odule">

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Object.prototype.toLocaleString()

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

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"

構文

js
toLocaleString()

引数

なし。ただし、このメソッドをオーバーライドするすべてのオブジェクトは、 Number.prototype.toLocaleString のように localesoptions に対応する最大 2 つの引数を受け取ることが期待されます。これらの引数の位置は、他の目的には使用しないでください。

返値

this.toString() 呼び出しの返値です。

解説

Object.prototype 継承するすべてのオブジェクト(つまり、 null プロトタイプオブジェクトを除くすべてのオブジェクト)は、 toLocaleString() メソッドを継承します。 Object's toLocaleString は、 this.toString() を呼び出した結果を返します。

この関数は、オブジェクトに汎用的な toLocaleString メソッドを提供するために用意されています。コア言語では、これらの組み込みオブジェクトは toLocaleString をオーバーライドしてロケール特有の書式設定を行います。

基底の toLocaleString() メソッドの使用

基底の toLocaleString() メソッドは、単純に toString() を呼び出します。

js
const obj = {
  toString() {
    return "My Object";
  },
};
console.log(obj.toLocaleString()); // "My Object"

Array における toLocaleString() のオーバーライド

Array.prototype.toLocaleString()は、各要素の toLocaleString() メソッドを呼び出して、結果をロケール特有の区切り文字で連結することで、配列の値を文字列として出力するために使用されます。例を示します。

js
const testArray = [4, 7, 10];

const euroPrices = testArray.toLocaleString("fr", {
  style: "currency",
  currency: "EUR",
});
// "4,00 €,7,00 €,10,00 €"

Date における toLocaleString() の上書き

Date.prototype.toLocaleString() は、特定のロケールに適した日付表示を出力するために使用されます。例を示します。

js
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 における toLocaleString() の上書き

Number.prototype.toLocaleString() は、特定のロケールに適した数値表示を出力するために使用されます。例を示します。

js
const testNumber = 2901234564;
// "2901234564"

const deNumber = testNumber.toLocaleString("de");
// "2.901.234.564"

const frNumber = testNumber.toLocaleString("fr");
// "2 901 234 564"

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-object.prototype.tolocalestring

ブラウザーの互換性

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy