From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../number/tolocalestring/index.html | 191 +++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/number/tolocalestring/index.html (limited to 'files/ja/web/javascript/reference/global_objects/number/tolocalestring') diff --git a/files/ja/web/javascript/reference/global_objects/number/tolocalestring/index.html b/files/ja/web/javascript/reference/global_objects/number/tolocalestring/index.html new file mode 100644 index 0000000000..7b70d30c40 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/number/tolocalestring/index.html @@ -0,0 +1,191 @@ +--- +title: Number.prototype.toLocaleString() +slug: Web/JavaScript/Reference/Global_Objects/Number/toLocaleString +tags: + - Internationalization + - JavaScript + - Method + - Number + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toLocaleString +--- +
{{JSRef}}
+ +

toLocaleString() メソッドは、この数値を表す言語依存の文字列を返します。

+ +
{{EmbedInteractiveExample("pages/js/number-tolocalestring.html")}}
+ + + +

新しい localesoptions 引数で アプリケーションは フォーマット変換で使われる言語を指定でき、関数の振る舞いをカスタマイズできます。古い実装では、localesoptions 引数は無視され、使われるロケールや返される文字列の形式は完全に実装依存です。

+ +

構文

+ +
numObj.toLocaleString([locales [, options]])
+ +

引数

+ +

どのブラウザーが locales 引数と options 引数をサポートしているか確かめるためにブラウザー実装状況セクションを調べてください。特徴検出のために例: locales 引数と options 引数をサポートしているか調べるを調べてください。

+ +
+

注意: Firefox 29 で実装されている ECMAScript 国際化 API では、locales 引数が Number.toLocaleString() メソッドに追加されました。引数が {{jsxref("undefined")}} なら、このメソッドは OS によって指定されたローカライズされた数値を返します。Firefox の以前のバージョンでは、英語の数字が返されます。この変更はすぐに修正される可能性がある下位互換性に影響を与える回帰として報告されています。({{bug(999003)}})

+
+ +
{{page('/ja/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', 'Parameters')}}
+ +

戻り値

+ +

渡された数値を表す、言語依存の文字列です。

+ +

+ +

toLocaleString を使う

+ +

ロケールを指定しない基本的な使用で、デフォルトロケールとデフォルトオプションのフォーマットされた文字列が返されます。

+ +
var number = 3500;
+
+console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale
+
+ +

locales 引数と options 引数をサポートしているか調べる

+ +

locales 引数と options 引数はまだすべてのブラウザーでサポートされておりません。不正な言語タグが {{jsxref("Global_Objects/RangeError", "RangeError")}} 例外で拒否される要件を使うことで、実装がすでにサポートしているかどうかを調べられます。

+ +
function toLocaleStringSupportsLocales() {
+  var number = 0;
+  try {
+    number.toLocaleString('i');
+  } catch (e) {
+    return e​.name === 'RangeError';
+  }
+  return false;
+}
+
+ +

ES5.1 以前の実装では、引数を使って toLocaleString を呼んだ場合に {{jsxref("RangeError")}} 例外を throw する必要はありませんでした。

+ +

5.1 以前の ECMAScript をサポートしているものも含めたすべてのホストで動くチェックは、直接 Number.prototype.toLocaleString の地域オプションのサポートに必要な ECMA-402 で指定された機能をテストすることで行えます。

+ +
function toLocaleStringSupportsOptions() {
+  return !!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function');
+}
+ +

上記のコードは、グローバル Intl オブジェクトが null でないことと、Intl オブジェクトが NumberFormat プロパティを持ち、それが関数であることをテストします。

+ +

locales を使う

+ +

この例ではローカライズされた数値変換のバリエーションのいくつかを示します。アプリケーションのユーザーインターフェイスで使われる言語の形式を得るために、locales 引数を用いている言語(そしておそらくいくつかのフォールバック言語)を明示することを確かめてください。

+ +
var number = 123456.789;
+
+// German uses comma as decimal separator and period for thousands
+console.log(number.toLocaleString('de-DE'));
+// → 123.456,789
+
+// Arabic in most Arabic speaking countries uses real Arabic digits
+console.log(number.toLocaleString('ar-EG'));
+// → ١٢٣٤٥٦٫٧٨٩
+
+// India uses thousands/lakh/crore separators
+console.log(number.toLocaleString('en-IN'));
+// → 1,23,456.789
+
+// the nu extension key requests a numbering system, e.g. Chinese decimal
+console.log(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
+
+ +

options を使う

+ +

toLocaleString によって得られる結果は options 引数を使用してカスタマイズできます。

+ +
var number = 123456.789;
+
+// request a currency format
+console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
+// → 123.456,79 €
+
+// the Japanese yen doesn't use a minor unit
+console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
+// → ¥123,457
+
+// limit to three significant digits
+console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
+// → 1,23,000
+
+// Use the host default language with options for number formatting
+var num = 30000.65;
+console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));
+// → "30,000.65" where English is the default language, or
+// → "30.000,65" where German is the default language, or
+// → "30 000,65" where French is the default language
+
+ +

性能

+ +

多数の数値をフォーマットするとき、{{jsxref("NumberFormat")}} オブジェクトを生成して {{jsxref("NumberFormat.format")}} プロパティによって得られる関数を使用するほうが良いです。

+ +

仕様

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
仕様書状況コメント
{{SpecName('ES3')}}{{Spec2('ES3')}}初期定義です。JavaScript 1.5 で実装されました。
{{SpecName('ES5.1', '#sec-15.7.4.3', 'Number.prototype.toLocaleString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-number.prototype.tolocalestring', 'Number.prototype.toLocaleString')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-number.prototype.tolocalestring', 'Number.prototype.toLocaleString')}}{{Spec2('ESDraft')}} 
{{SpecName('ES Int 1.0', '#sec-13.2.1', 'Number.prototype.toLocaleString')}}{{Spec2('ES Int 1.0')}} 
{{SpecName('ES Int 2.0', '#sec-13.2.1', 'Number.prototype.toLocaleString')}}{{Spec2('ES Int 2.0')}} 
{{SpecName('ES Int Draft', '#sec-Number.prototype.toLocaleString', 'Number.prototype.toLocaleString')}}{{Spec2('ES Int Draft')}} 
+ +

ブラウザー実装状況

+ + + +

{{Compat("javascript.builtins.Number.toLocaleString")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf