Skip to content

Commit

Permalink
update number/tolocalestring (#8828)
Browse files Browse the repository at this point in the history
* update number/tolocalestring

Remove `{{page}}` occurence and update with newest EN document.

Related issue: #3893

* Apply suggestions from code review

* Update index.md

* Apply suggestions from code review

Co-authored-by: A1lo <yin199909@aliyun.com>

Co-authored-by: A1lo <yin199909@aliyun.com>
  • Loading branch information
jasonren0403 and yin1999 authored Sep 26, 2022
1 parent f78c589 commit da2f07b
Showing 1 changed file with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,94 @@ title: Number.prototype.toLocaleString()
slug: Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
---

{{JSRef("Global_Objects", "Number")}}
{{JSRef}}

**`toLocaleString()`** 方法返回这个数字在特定语言环境下的表示字符串。
**`toLocaleString()`** 方法返回这个数字在特定语言环境下的表示字符串。在使用具有 [`Intl.NumberFormat` API](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 支持的实现时,此方法仅仅简单调用了 `Intl.NumberFormat`

新的 `locales``options` 参数让应用程序可以指定要进行格式转换的语言,并且定制函数的行为。在旧的实现中,会忽略 `locales``options` 参数,使用的语言环境和返回的字符串的形式完全取决于实现方式。
{{EmbedInteractiveExample("pages/js/number-tolocalestring.html")}}

## 语法

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

### 参数

查阅[浏览器兼容性](#浏览器兼容性)部分,了解哪些浏览器支持 `locales``options` 参数,通过[示例:检查 `locales``options` 参数的支持](#Checking_for_support_for_locales_and_options_arguments)了解特征检测
`locales``options` 参数让应用程序可以指定要进行格式转换的语言,并且定制函数的行为

> **备注:** ECMAScript 国际化 API,在 Firefox 29 中得以实施,增加了 `locales` 参数的 [`Number.toLocaleString`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) 方法。如果参数为 `undefined`,此方法返回本地操作系统指定的位数,而 Firefox 的早期版本中返回[阿拉伯语](https://en.wikipedia.org/wiki/Arabic_numerals)数字。这一变化已被报告为向后影响的兼容性问题并可能会被尽快修复。({{ bug(999003) }})
在使用具有 [`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` 时,会忽略参数,使得使用的区域设置和返回的字符串格式完全由实现决定。

{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}
- `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)以了解这些参数的详细信息,以及如何使用它们。

### 返回值

返回一个语言环境下的表示字符串。

在使用 `Intl.NumberFormat` 的实现中,这与 `new Intl.NumberFormat(locales, options).format(number)` 调用等价。

## 性能

当格式化非常多的数字时,最好创建一个 {{jsxref("Intl.NumberFormat")}} 对象,并且使用其 {{jsxref("Intl/NumberFormat/format", "format")}} 属性提供的方法。

## 示例

### 使用 `toLocaleString`
### 使用 toLocaleString()

在没有指定区域的基本使用时,返回使用默认的语言环境和默认选项格式化的字符串。

```js
var number = 3500;
const number = 3500;

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

### 检查 `locales``options` 参数的支持

`locales``options` 参数目前还不是所有浏览器都支持的。在 ES5.1 和更新的实现中检查支持情况,可以依靠使用非法参数时规定抛出的 {{jsxref("Global_Objects/RangeError", "RangeError")}} 异常
并不是所有实现都需要支持 ECMA-402 标准(国际化 API)。如果浏览器不支持该标准,则参数 `locales``options` 参数必须都要忽略掉。可以依靠使用非法参数时规定抛出的 {{jsxref("Global_Objects/RangeError", "RangeError")}} 异常来检测支持的情况

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

早于 ES5.1 的实现中,如果带参数调用 `toLocaleString` 并不会抛出范围异常。

在所有宿主环境下,包括那些支持比 ed 5.1 还早的 ECMA-262 的环境,都能有效检测的方法是直接检测 ECMA-402 中的其它特性,它指定 `Number.prototype.toLocaleString` 需要支持地区选项:
早于 ES5.1 的实现中,携带非法参数调用 `toLocaleString` 不需要抛出范围异常。在所有主机环境下,包括那些支持比 ed 5.1 还早的 ECMA-262 的环境,都能有效检测的方法是直接检测 ECMA-402 中的其它特性,它指定 `Number.prototype.toLocaleString` 需要支持地区选项:

```js
function toLocaleStringSupportsOptions() {
return !!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function');
return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function');
}
```

它测试全局的 `Intl` 对象,检测它不是 `null` 并且有 `NumberFormat` 的方法。

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

这个示例展示了不同地区数字格式的差异。为了设置你的应用程序界面下使用的语言格式,请确保使用 `locales` 参数指定了使用的语言(可能还有一些备用语言):

```js
var number = 123456.789;
const number = 123456.789;

// 德国使用逗号作为小数分隔符,分位周期为千位
console.log(number.toLocaleString('de-DE'));
Expand All @@ -95,12 +113,12 @@ console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789
```

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

通过 `toLocaleString` 返回的结果可以通过 `options` 参数进行定制:

```js
var number = 123456.789;
const number = 123456.789;

// 要求货币格式
console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
Expand All @@ -113,11 +131,14 @@ console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY'
// 限制三位有效数字
console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
// → 1,23,000
```

## 性能

当格式化大量数字时,最好建立一个 {{jsxref("Global_Objects/NumberFormat", "NumberFormat")}} 对象并且使用它提供的 {{jsxref("NumberFormat.format")}} 方法。
// 使用带选项的主机默认语言进行数字格式化
const num = 30000.65;
console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));
// → "30,000.65" 英语为默认语言
// → "30.000,65" 德语为默认语言
// → "30 000,65" 法语为默认语言
```

## 规范

Expand All @@ -127,6 +148,6 @@ console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));

{{Compat}}

## 相关链接
## 参见

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

0 comments on commit da2f07b

Please sign in to comment.