Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Number.prototype.toLocaleString()

この記事は編集レビューを必要としています。ぜひご協力ください

この翻訳は不完全です。英語から この記事を翻訳 してください。

概要

このNumberオブジェクトのローケルに応じた文字列表現を返します。

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

構文

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

引数

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

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

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, see the Intl page. The following Unicode extension key is allowed:

nu
The numbering system to be used. Possible values include: "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt".
options

Optional. An object with some or all of the following properties:

localeMatcher
The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.
style
The formatting style to use. Possible values are "decimal" for plain number formatting, "currency" for currency formatting, and "percent" for percent formatting; the default is "decimal".
currency
The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB — see the Current currency & funds code list. There is no default value; if the style is "currency", the currency property must be provided.
currencyDisplay
How to display the currency in currency formatting. Possible values are "symbol" to use a localized currency symbol such as €, "code" to use the ISO currency code, "name" to use a localized currency name such as "dollar"; the default is "symbol".
useGrouping
Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. Possible values are true and false; the default is true.

The following properties fall into two groups: minimumIntegerDigits, minimumFractionDigits, and maximumFractionDigits in one group, minimumSignificantDigits and maximumSignificantDigits in the other. If at least one property from the second group is defined, then the first group is ignored.

minimumIntegerDigits
The minimum number of integer digits to use. Possible values are from 1 to 21; the default is 1.
minimumFractionDigits
The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).
maximumFractionDigits
The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information); the default for percent formatting is the larger of minimumFractionDigits and 0.
minimumSignificantDigits
The minimum number of significant digits to use. Possible values are from 1 to 21; the default is 1.
maximumSignificantDigits
The maximum number of significant digits to use. Possible values are from 1 to 21; the default is minimumSignificantDigits.

例: toLocaleStringを使う

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

var number = 3500;

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

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

locales引数と options引数はまだすべてのブラウザでサポートされておりません。実装がすでにサポートしているかどうか調べるために、 RangeError例外で違法な言語タグが拒否される要件を使用して下さい。:

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

例: 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

性能

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

使用

使用 状況 コメント
ECMAScript 3rd Edition. Standard Initial definition. Implemented in JavaScript 1.5.
ECMAScript 5.1 (ECMA-262)
The definition of 'Number.prototype.toLocaleString' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Number.prototype.toLocaleString' in that specification.
Standard  
ECMAScript Internationalization API 1.0 (ECMA-402)
The definition of 'Number.prototype.toLocaleString' in that specification.
Standard  

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
基本サポート (有) (有) (有) (有) (有)
locales and options arguments 24 29 (29) 11 15 未サポート
機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート (有) (有) (有) (有) (有) (有)
locales and options arguments 未サポート 26 未サポート
バグ 864843
未サポート 未サポート 未サポート

関連情報

ドキュメントのタグと貢献者

 このページの貢献者: shide55
 最終更新者: shide55,