Intl.NumberFormat.prototype.format()

Intl.NumberFormat 实例的 format() 方法根据这个 Intl.NumberFormat 对象的 区域设置和格式选项 格式化一个数字。

¥The format() method of Intl.NumberFormat instances formats a number according to the locale and formatting options of this Intl.NumberFormat object.

Try it

语法

¥Syntax

js
format(number)

参数

¥Parameters

number

要格式化的 NumberBigInt 或字符串。字符串的解析方式与 数字转换 中相同,只是 format() 将使用字符串表示的确切值,避免隐式转换为数字期间的精度损失。

注意:旧版本的规范将字符串解析为 Number。检查你的浏览器的兼容性表。

¥Note: Older versions of the specification parsed strings as a Number. Check the compatibility table for your browser.

返回值

¥Return value

表示给定 number 的字符串,根据该 Intl.NumberFormat 对象的区域设置和格式选项进行格式化。

¥A string representing the given number formatted according to the locale and formatting options of this Intl.NumberFormat object.

描述

¥Description

JavaScript 中的 Number 值如果太大或太小,就会导致精度损失,从而导致文本表示不准确。如果你使用大于 Number.MAX_SAFE_INTEGER 的整数进行计算,则应使用 BigInt,这样格式会正确:

¥Number values in JavaScript suffer from loss of precision if they are too big or too small, making the text representation inaccurate. If you are performing calculations with integers larger than Number.MAX_SAFE_INTEGER you should use a BigInt instead, which will format correctly:

js
new Intl.NumberFormat("en-US").format(1234567891234567891); // 1,234,567,891,234,568,000
new Intl.NumberFormat("en-US").format(1234567891234567891n); // 1,234,567,891,234,567,891

你还可以传递非常大的字符串以将其格式化为任意精度的十进制字符串(如果你正在对数据执行计算,你仍然需要使用 BigInt):

¥You can also pass through very large strings to be formatted as an arbitrary-precision decimal string (if you're performing calculations on the data you will still need to work with BigInt):

js
new Intl.NumberFormat("en-US").format("1234567891234567891"); // 1,234,567,891,234,567,891

示例

¥Examples

使用格式

¥Using format

使用 format getter 函数格式化单个货币值。下面的代码显示了如何针对俄罗斯语言环境设置卢布货币的格式:

¥Use the format getter function for formatting a single currency value. The code below shows how to format the roubles currency for a Russian locale:

js
const options = { style: "currency", currency: "RUB" };
const numberFormat = new Intl.NumberFormat("ru-RU", options);
console.log(numberFormat.format(654321.987));
// "654 321,99 ₽"

使用映射格式

¥Using format with map

使用 format getter 函数格式化数组中的所有数字。请注意,该函数绑定到从中获取它的 Intl.NumberFormat,因此可以将其直接传递给 Array.prototype.map。这被认为是历史文物,作为新功能不再遵循的惯例的一部分,但被保留以保持与现有程序的兼容性。

¥Use the format getter function for formatting all numbers in an array. Note that the function is bound to the Intl.NumberFormat from which it was obtained, so it can be passed directly to Array.prototype.map. This is considered a historical artefact, as part of a convention which is no longer followed for new features, but is preserved to maintain compatibility with existing programs.

js
const a = [123456.789, 987654.321, 456789.123];
const numberFormat = new Intl.NumberFormat("es-ES");
const formatted = a.map((n) => numberFormat.format(n));
console.log(formatted.join("; "));
// "123.456,789; 987.654,321; 456.789,123"

使用带有字符串的格式

¥Using format with a string

使用字符串,我们可以指定大于 Number.MAX_SAFE_INTEGER 的非常多的数字,而不会损失精度。

¥Using a string we can specify very numbers that are larger than Number.MAX_SAFE_INTEGER without losing precision.

js
const numberFormat = new Intl.NumberFormat("en-US");

// Here the value is converted to a Number
console.log(numberFormat.format(987654321987654321));
// 987,654,321,987,654,300

// Here we use a string and don't lose precision
console.log(numberFormat.format("987654321987654321"));
// 987,654,321,987,654,321

我们还可以对十进制字符串使用通用 "E" 指数语法:#.#E#。下面的代码创建一个 BigInt,将其强制转换为后缀为 E-6 的字符串,然后对其进行格式化。

¥We can also use the general "E" exponent syntax for decimal strings: #.#E#. The code below creates a BigInt, coerces it to a string with the suffix E-6, and then formats it.

js
const numberFormat = new Intl.NumberFormat("en-US");
const bigNum = 1000000000000000110000n;
console.log(numberFormat.format(bigNum));
// "1,000,000,000,000,000,110,000"

// Format as a string using the `E` syntax:
console.log(numberFormat.format(`${bigNum}E-6`));
// "1,000,000,000,000,000.11"

规范

Specification
ECMAScript Internationalization API Specification
# sec-intl.numberformat.prototype.format

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看