Intl.NumberFormat.prototype.format()
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
* Some parts of this feature may have varying levels of support.
Intl.NumberFormat.prototype.format()
メソッドは、この Intl.NumberFormat
オブジェクトのロケールと整形オプションに従って数値を整形します。
試してみましょう
const amount = 654321.987; const options1 = { style: "currency", currency: "RUB" }; const numberFormat1 = new Intl.NumberFormat("ru-RU", options1); console.log(numberFormat1.format(amount)); // Expected output: "654 321,99 ₽" const options2 = { style: "currency", currency: "USD" }; const numberFormat2 = new Intl.NumberFormat("en-US", options2); console.log(numberFormat2.format(amount)); // Expected output: "$654,321.99"
構文
js
format(number);
引数
解説
format
ゲッター関数は、この NumberFormat
オブジェクトのロケールと整形オプションに従って数値を整形し、文字列に格納します。
例
format
の使用
format
ゲッター関数を使用して単一の通貨値を整形しましょう。こちらはロシアの例です。
js
var options = { style: "currency", currency: "RUB" }; var numberFormat = new Intl.NumberFormat("ru-RU", options); console.log(numberFormat.format(654321.987)); // → "654 321,99 руб."
format
と map
の使用
format
ゲッター関数を使用して、配列内のすべての数値を整形することができます。 なお、この関数は供給元である Intl.NumberFormat
に結び付けられているので、直接 Array.prototype.map
に渡すことができます。これは、新しい機能に従わない習慣の一部として、歴史的な人工物と考えられていますが、既存のプログラムとの互換性のために保守されています。
js
var a = [123456.789, 987654.321, 456789.123]; var numberFormat = new Intl.NumberFormat("es-ES"); var formatted = a.map((n) => numberFormat.format(n)); console.log(formatted.join("; ")); // → "123.456,789; 987.654,321; 456.789,123"
仕様書
Specification |
---|
ECMAScript® 2026 Internationalization API Specification # sec-intl.numberformat.prototype.format |