From e9cfe52147fd41c1855720914fbaf645f36e72b2 Mon Sep 17 00:00:00 2001 From: allo Date: Fri, 16 Dec 2022 08:57:43 +0800 Subject: [PATCH 1/2] sync with english version for BigInt.toLocaleString() --- .../bigint/tolocalestring/index.md | 98 +++++++++++-------- 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md index 2169f3fba9c510..0d1b11428cbfbc 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md @@ -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` 方法。 {{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}} @@ -99,6 +117,6 @@ console.log(bigint.toLocaleString('en-IN', { maximumSignificantDigits: 3 })); {{Compat}} -## 请参阅 +## 参见 -- {{jsxref("BigInt.toString()")}} +- {{jsxref("BigInt.prototype.toString()")}} From cd3e185fd3980cc0cacae6bb7d34d27ed249b560 Mon Sep 17 00:00:00 2001 From: A1lo Date: Fri, 16 Dec 2022 13:33:30 +0800 Subject: [PATCH 2/2] fix typo --- .../reference/global_objects/bigint/tolocalestring/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md index 0d1b11428cbfbc..7ce18bbc630a7c 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.md @@ -5,7 +5,7 @@ slug: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString {{JSRef}} -**`toLocaleString()`** 方法返回一个表示给定 BitInt 对象的字符串,该字符串格式因不同语言而不同。在支持 [`Intl.NumberFormat` API](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 的实现中,该方法仅是调用了 `Intl.NumberFormat` 方法。 +**`toLocaleString()`** 方法返回一个表示给定 BigInt 对象的字符串,该字符串格式因不同语言而不同。在支持 [`Intl.NumberFormat` API](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 的实现中,该方法仅是调用了 `Intl.NumberFormat` 方法。 {{EmbedInteractiveExample("pages/js/bigint-tolocalestring.html")}}