JavaScript Number.toLocaleString() Method
Example
Formats a string using local settings:
let
num1
=
1000000
;
let
text1
=
num1
.
toLocaleString
(
)
;
//
Formats a number as a string using a specific locale
let
num2
=
1000000
;
let
text2
=
num2
.
toLocaleString
(
"
fi-FI
"
)
;
//
Formats a number as a currency string using a specific locale
//
Renminbi (CNY)
let
num3
=
1000000
;
let
text3
=
num3
.
toLocaleString
(
"
zh-CN
"
,
{
style
:
"
currency
"
,
currency
:
"
CNY
"
}
)
;
//
US Dollar (USD)
let
num4
=
1000000
;
let
text4
=
num4
.
toLocaleString
(
"
en-US
"
,
{
style
:
"
currency
"
,
currency
:
"
USD
"
}
)
;
Try it »
Definition and Usage
The toLocaleString() method returns a string representing the number in a specific locale.
The new locales and options parameters let applications specify the language for formatting and customize the function's behavior. In older implementations, the locales and options parameters are ignored, and the locale used and the form of the returned string depend entirely on the implementation.
Browser support
Number.toLocaleString() is an ECMAScript 3 (ES3) feature.
All browsers support ES3 (JavaScript 1999).
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes | Yes |
The latest browser versions support the (locales, options) parameters:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 11 | Yes | Yes | Yes | Yes |
Syntax
number.toLocaleString(locales, options)
Parameter value
| Parameters | Description |
|---|---|
| * locales * | Optional, a formatting object, which can be: |
`ar-SA`阿拉伯 (沙特阿拉伯)
`bn-BD`孟加拉语(孟加拉国)
`bn-IN`孟加拉国(印度)
`cs-CZ`捷克语(捷克共和国)
`da-DK`丹麦语(丹麦)
`de-AT`奥地利德语
`de-CH`"瑞士"德语
`de-DE`标准德语
`el-GR`现代希腊语
`en-AU`澳大利亚英语
`en-CA`加拿大英语
`en-GB`英式英语
`en-IE`爱尔兰英语
`en-IN`印度英语
`en-NZ`新西兰英语
`en-US`美国英语
`en-ZA`英语(南非)
`es-AR`阿根廷 西班牙语
`es-CL`智利 西班牙语
`es-CO`哥伦比亚西班牙语
`es-ES`卡斯蒂利亚西班牙语(在西班牙中北部使用)
`es-MX`墨西哥西班牙语
`es-US`式西班牙语
`fi-FI`芬兰语(芬兰)
`fr-BE`比利时法语
`fr-CA`加拿大法语
`fr-CH`"瑞士"法语
`fr-FR`标准法语(在法国)
`he-IL`希伯来语(以色列)
`hi-IN`印地语(印度)
`hu-HU`匈牙利语(匈牙利)
`id-ID`印度尼西亚语(印度尼西亚语)
`it-CH`"瑞士"意大利语
`it-IT`标准意大利语(在意大利使用)
`ja-JP`日语(日本)
`ko-KR`韩语(韩国)
`nl-BE`比利时荷兰语
`nl-NL`标准荷兰语
`no-NO`挪威语(挪威)
`pl-PL`波兰语(波兰)
`pt-BR`巴西葡萄牙语
`pt-PT`欧洲葡萄牙语
`ro-RO`罗马尼亚语(罗马尼亚)
`ru-RU`俄语(俄罗斯联邦)
`sk-SK`斯洛伐克语(斯洛伐克)
`sv-SE`瑞典语(瑞典)
`ta-IN`印度泰米尔语
`ta-LK`斯里兰卡泰米尔语
`th-TH`泰语(泰国)
`tr-TR`土耳其语(土耳其)
`zh-CN`中国大陆,简体字
`zh-HK`香港地区,繁体字
`zh-TW`台湾地区,繁体字 |
| * options * | Optional, can be:
"decimal"Used for plain number format;"currency"Used for currency format;"percent"Used for percent format;"unit"Used for unit format
Return Value
| Type | Description |
|---|---|
| String | Returns a representation string in the given locale. |
Technical Details
| JavaScript version: | ECMAScript 6 |
|---|
More examples
Example
Using optional parameters to format a currency string:
let
num1
=
new
Number
(
1000000
)
;
const
myObj
=
{
style
:
"
currency
"
,
currency
:
"
EUR
"
}
let
text1
=
num1
.
toLocaleString
(
"
en-GB
"
,
myObj
)
;
let
num2
=
new
Number
(
1000000
)
;
let
text2
=
num2
.
toLocaleString
(
"
en-GB
"
,
{
style
:
"
currency
"
,
currency
:
"
EUR
"
}
)
;
//
Renminbi (CNY)
let
num3
=
1000000
;
let
text3
=
num3
.
toLocaleString
(
"
zh-CN
"
,
{
style
:
"
currency
"
,
currency
:
"
CNY
"
}
)
;
//
Japanese Yen (JPY)
let
num4
=
1000000
;
let
text4
=
num4
.
toLocaleString
(
"
ja-JP
"
,
{
style
:
"
currency
"
,
currency
:
"
JPY
"
}
)
;
Try it »
JavaScript Number object