Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync with english version for BigInt.toLocaleString() #10666

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,110 @@ slug: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString

{{JSRef}}

**`toLocaleString()`** 方法返回一个字符串,该字符串具有此 `BigInt` 的 language-sensitive 表达形式
**`toLocaleString()`** 方法返回一个表示给定 BitInt 对象的字符串,该字符串格式因不同语言而不同。在支持 [`Intl.NumberFormat` API](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 的实现中,该方法仅是调用了 `Intl.NumberFormat` 方法
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

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

## 语法

```plain
bigIntObj.toLocaleString([locales [, options]])
```js-nolint
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
```

### 参数

`locales` 和 `options` 参数可自定义函数的行为,并允许应用程序指定应使用其格式约定的语言。在忽略 `locales` 和 `options` 参数的实现中,使用的 `locale` 和返回的字符串形式完全依赖于实现
`locales` 和 `options` 参数使程序能够指定使用哪种语言格式化规则,允许定制该方法的行为(behavior)

{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}
在支持 [`Intl.NumberFormat` API](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 的实现中,这些参数与 [`Intl.NumberFormat()`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) 构造函数的参数完全对应。而对于不支持 `Intl.NumberFormat` 的实现,则要求函数忽略这两个参数,使得函数使用的区域(locale)以及返回的字符串的格式完全取决于实现。

- `locales` {{optional_inline}}

- : 表示缩写语言代码(BCP 47 language tag)的字符串,或由此类字符串组成的数组。对应于 `Intl.NumberFormat()` 构造函数的 [`locales`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#locales) 参数。

在不支持 `Intl.NumberFormat` 的实现中,该参数会被忽略,并且通常会使用主机的区域设置。

- `options` {{optional_inline}}

- : 一个调整输出格式的对象。对应于 `Intl.NumberFormat()` 构造函数的 [`options`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) 参数。

在不支持 `Intl.NumberFormat` 的实现中,该参数会被忽略。

参见 [`Intl.NumberFormat()` 构造函数](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat)以详细了解这些参数以及如何使用它们。

### 返回值

具有此 `BigInt` 的 language-sensitive 表示形式的字符串
一个表示给定 BitInt 对象的字符串,该字符串格式因不同语言而不同

## 例子
在支持 `Intl.NumberFormat` 的实现中,此方法等价于 `new Intl.NumberFormat(locales, options).format(number)`。

### Using `toLocaleString`
## 性能

当需要对大量的数字进行格式化时,最好创建一个 {{jsxref("Intl.NumberFormat")}} 对象,并使用其 {{jsxref("Intl/NumberFormat/format", "format()")}} 方法提供的函数。

## 示例

### 使用 `toLocaleString`

在不指定语言环境的基本用法中,将返回默认语言环境中带默认选项的格式化字符串
没有指定区域(locale)时,返回一个使用默认区域和格式设置(options)的格式化字符串

```js
var bigint = 3500n;
const bigint = 3500n;

bigint.toLocaleString();
// Displays "3,500" if in U.S. English locale
console.log(bigint.toLocaleString());
// "3,500" 如果区域为 en-US
```

### Using `locales`
### 使用 `locales`

这个例子展示了本地化数字格式的一些变体。为了获得应用程序用户界面中使用的语言的格式,请确保使用 `locales` 参数指定该语言(可能还有一些备用语言):
这个例子展示了本地化数字格式的一些变体。为了获得应用程序用户界面中使用的语言的格式,请确保使用 `locales` 参数指定该语言(可能还有一些回退语言):

```js
var bigint = 123456789123456789n;
const bigint = 123456789123456789n;

// German uses period for thousands
// 德国使用句号表示千位
console.log(bigint.toLocaleString('de-DE'));
// 123.456.789.123.456.789
// 123.456.789.123.456.789

// Arabic in most Arabic speaking countries uses Eastern Arabic digits
// 大多数说阿拉伯语的阿拉伯国家使用东阿拉伯数字
console.log(bigint.toLocaleString('ar-EG'));
// ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩
// ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩

// India uses thousands/lakh/crore separators
// 印度使用千、万、千万分隔符
console.log(bigint.toLocaleString('en-IN'));
// 1,23,45,67,89,12,34,56,789
// 1,23,45,67,89,12,34,56,789

// the nu extension key requests a numbering system, e.g. Chinese decimal
// nu 扩展键要求使用编号系统,例如中文十进制数
console.log(bigint.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(bigint.toLocaleString(['ban', 'id']));
// 123.456.789.123.456.789
// 123.456.789.123.456.789
```

### Using `options`
### 使用 `options`

`toLocaleString` 提供的结果可以使用 `options` 参数进行自定义:

```js
var bigint = 123456789123456789n;
const bigint = 123456789123456789n;

// request a currency format
// 要求使用货币格式
console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// 123.456.789.123.456.789,00 €
// 123.456.789.123.456.789,00 €

// the Japanese yen doesn't use a minor unit
// 日元不使用小数单位
console.log(bigint.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
// ¥123,456,789,123,456,789
// ¥123,456,789,123,456,789

// limit to three significant digits
// 保留三位有效数字
console.log(bigint.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
// 1,23,00,00,00,00,00,00,000
// 1,23,00,00,00,00,00,00,000
```

## 性能

格式化大量数字时,最好创建 {{jsxref("NumberFormat")}} 对象并使用其 {{jsxref("NumberFormat.format")}} 属性提供的函数。

## 规范

{{Specifications}}
Expand All @@ -99,6 +117,6 @@ console.log(bigint.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));

{{Compat}}

## 请参阅
## 参见

- {{jsxref("BigInt.toString()")}}
- {{jsxref("BigInt.prototype.toString()")}}