The toLocaleString() method returns the string representing the number in a specific locale. By calling the toLocaleString() method of a number object (or number literal) and specifying a specific locale parameter, we can easily get a more suitable value.
grammar:
numObj.toLocaleString([locales [, options]])
locales:
optional. A string of abbreviated language codes (BCP 47 language tag, eg: cmn-Hans-CN) or an array of these strings.
The following extended usage is language[-scripts][-region]-u-nu-*, for example: zh-u-nu-hanidec (indicating Chinese decimal numbers).
not
The numbering system to use. Possible values are: "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec" (Chinese decimal), "khmr" , "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt".
options
Optional. A class containing some or all of the following properties:
"decimal" for pure number format;
"currency" for currency format;
"percent" for percentage format;
"unit" for unit format
localeMatcher
uses the local matching algorithm. Possible values are "lookup" and "best fit "; The default is "best fit". For more information on this option, see the Intl page.
style
The format style to use, defaults to "decimal".
numberingSystem
numbering system. Possible values include: "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", " laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt".
unit
unit The unit used in the format, possible values are the core unit identifiers defined in UTS #35, Part 2, Section 6. A subset of units has been selected from the full list for use with ECMAScript. Pairs of simple units can be concatenated with "-per-" to form a compound unit. There is no default; if style is "unit", the unit attribute must be provided.
unitDisplay
The unit formatting style used in unit formatting, default is 'short'.
“long” (e.g., 16 litres)
“short“ (e.g., 16 l)
”narrow“ (e.g., 16l)
currency
The currency symbol to use in currency formatting. Possible values are ISO currency codes (the ISO 4217 currency codes,) such as "USD" for US dollars, "EUR" for euros, or "CNY" for Chinese yuan — see more Current currency & funds code list. There is no default value, if the style is "currency", the currency attribute must be provided.
currencyDisplay
How to display currency in currency formatting. Possible values are "symbol" to use a localized currency symbol such as €, "code" to use an ISO currency code, and "name" to use a localized currency name such as "dollar"; default value is "symbol".
useGrouping
Whether to use a grouping separator, such as thousands separator or thousand/million/billion separator. Possible values are true and false, default value is true.
The following properties are grouped into two groups: minimumintegerdigits, minimumfractiondigits, maximumfractiondigits as a group, minimumsignificantdigits and maximumsignificantdigits as a group. If any of the properties in the second group are defined, the settings in the first group are ignored.
minimumIntegerDigits
Minimum number of integer digits to use. Possible values are from 1 to 21, default is 1.
minimumFractionDigits
The minimum number of decimal places to use. Possible values are from 0 to 20; defaults to 0 for normal number and percentage formats; defaults to currency formats provided by the ISO 4217 currency code list (if not provided, the value is 2).
maximumFractionDigits
The maximum number of decimal places to use. Possible values are from 0 to 20; the default value for pure numeric format is the greater of minimumfractiondigits and 3; the default value for currency format is the greater of minimumfractiondigits and the ISO 4217 currency code list (if the list is not provided then the value is 2); the default value of the percentage format is the larger of minimumfractiondigits and 0.
minimumSignificantDigits
The minimum number of significant digits to use. Possible values are from 1 to 21; the default is 1.
maximumSignificantDigits
The maximum number of significant digits used. Possible values are from 1 to 21; default is 21.
notation
The format in which the number should be displayed, defaults to "standard".
"standard" pure number format;
"scientific" returns the magnitude order of the formatted number;
"engineering" returns the exponent of ten when divided by three
"compact" represents the string representing the exponent, the default uses the "short" format
"compactDisplay" only in Used when notation is "compact", takes "short" (default) or "long"
example
数字:
var a = 123456789;
console.log(a.toLocaleString()); // "123,456,789"
var number = 123456.789;
// 在德国点(.)号是作为千分位符号而逗号(,)则作为小数点。Orz
alert(number.toLocaleString("de-DE"));
// → 123.456,789
// 这才是真正的阿拉伯数字
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩
// 看看三哥的奇葩数字表示法
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789
// 大汉子勃大茎深
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九
// 当一个语言关键字可能不被支持的时候,还可以指定一个备用的关键字,比如下面情况:
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789
// request a currency format
alert(number.toLocaleString("de-DE", {
style: "currency", currency: "EUR"}));
// → 123.456,79 €
// the Japanese yen doesn't use a minor unit
alert(number.toLocaleString("ja-JP", {
style: "currency", currency: "JPY"}))
// → ¥123,457
// limit to three significant digits
alert(number.toLocaleString("en-IN", {
maximumSignificantDigits: 3}));
// → 1,23,000
数组:
var a = [1,2,3];
console.log(a.toLocaleString()); // "1,2,3" 与toString()的返回值一致
var b = [11111,new Date(),{
}];
console.log(b.toLocaleString()); // "11111,2016/2/23 下午12:31:09,[object Object]"
日期:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses month-day-year order and 12-hour time with AM/PM
console.log(date.toLocaleString('en-US'));
// → "12/19/2012, 7:00:00 PM"
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(date.toLocaleString('en-GB'));
// → "20/12/2012 03:00:00"
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(date.toLocaleString('ko-KR'));
// → "2012. 12. 20. 오후 12:00:00"
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleString('ar-EG'));
// → "٢٠/١٢/٢٠١٢ ٥:٠٠:٠٠ ص"
// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(date.toLocaleString('ja-JP-u-ca-japanese'));
// → "24/12/20 12:00:00"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(date.toLocaleString(['ban', 'id']));
// → "20/12/2012 11.00.00"