目录

JavaScript Number.toLocaleString()

示例

使用区域设置将数字格式化为字符串:

let num = 1000000;
let text = num.toLocaleString();
亲自试一试 »

使用芬兰特定的语言环境将数字格式化为字符串:

let num = 1000000;
let text = num.toLocaleString("fi-FI");
亲自试一试 »

使用美国特定的区域设置将数字格式化为货币字符串:

let num = 1000000;
let text = num.toLocaleString("en-US", {style:"currency", currency:"USD"});
亲自试一试 »

下面有更多示例


描述

这个toLocaleString()使用本地语言格式以字符串形式返回数字。

语言格式取决于计算机上的区域设置。


语法

number.toLocaleString( locales, options)

参数

Parameter Description
locales

尝试一下
Optional.
The language specific format to use.
Click on the "Try it" button to see all values in action.

ar-SA Arabic (Saudi Arabia)
bn-BD Bangla (Bangladesh)
bn-IN Bangla (India)
cs-CZ Czech (Czech Republic)
da-DK Danish (Denmark)
de-AT Austrian German
de-CH "Swiss" German
de-DE Standard German (as spoken in Germany)
el-GR Modern Greek
en-AU Australian English
en-CA Canadian English
en-GB British English
en-IE Irish English
en-IN Indian English
en-NZ New Zealand English
en-US US English
en-ZA English (South Africa)
es-AR Argentine Spanish
es-CL Chilean Spanish
es-CO Colombian Spanish
es-ES Castilian Spanish (as spoken in Central-Northern Spain)
es-MX Mexican Spanish
es-US American Spanish
fi-FI Finnish (Finland)
fr-BE Belgian French
fr-CA Canadian French
fr-CH "Swiss" French
fr-FR Standard French (especially in France)
he-IL Hebrew (Israel)
hi-IN Hindi (India)
hu-HU Hungarian (Hungary)
id-ID Indonesian (Indonesia)
it-CH "Swiss" Italian
it-IT Standard Italian (as spoken in Italy)
ja-JP Japanese (Japan)
ko-KR Korean (Republic of Korea)
nl-BE Belgian Dutch
nl-NL Standard Dutch (as spoken in The Netherlands)
no-NO Norwegian (Norway)
pl-PL Polish (Poland)
pt-BR Brazilian Portuguese
pt-PT European Portuguese (as written and spoken in Portugal)
ro-RO Romanian (Romania)
ru-RU Russian (Russian Federation)
sk-SK Slovak (Slovakia)
sv-SE Swedish (Sweden)
ta-IN Indian Tamil
ta-LK Sri Lankan Tamil
th-TH Thai (Thailand)
tr-TR Turkish (Turkey)
zh-CN Mainland China, simplified characters
zh-HK Hong Kong, traditional characters
zh-TW Taiwan, traditional characters

options Optional.
An object with formatting options:

currency Legal values: any currency code (like "EUR", "USD", "INR", etc.)
currencyDisplay
Legal values:
"symbol" (default)
"code"
"name"
localeMatcher
Legal values:
"best-fit" (default)
"lookup"
maximumFractionDigits
A number from 0 to 20 (default is 3)
maximumSignificantDigits
A number from 1 to 21 (default is 21)
minimumFractionDigits
A number from 0 to 20 (default is 3)
minimumIntegerDigits
A number from 1 to 21 (default is 1)
minimumSignificantDigits
A number from 1 to 21 (default is 21)
style
Legal values:
"currency"
"decimal" (default)
"percent"
useGrouping
Legal values:
"true" (default)
"false"

返回值

类型 描述
一个字符串 代表本地格式数字的字符串。


更多示例

使用选项参数(对象)进行货币格式化:

let num = new Number(1000000);
const myObj = {
  style: "currency",
  currency: "EUR"
}
let text = num.toLocaleString("en-GB", myObj);
亲自试一试 »
let num = new Number(1000000);
let text = num.toLocaleString("en-GB", {style:"currency", currency:"EUR"});
亲自试一试 »

使用日本特定的语言环境:

let num = 1000000;
let text = num.toLocaleString("ja-JP", {style:"currency", currency:"JPY"});
亲自试一试 »

浏览器支持

toLocaleString()是 ECMAScript3 (ES3) 功能。

所有浏览器均完全支持 ES3 (JavaScript 1999):

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

浏览器支持

(locales, options)所有现代浏览器都支持:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 11