toLocaleString() method returns a string representing the object. This method is meant to be overridden by derived objects for locale-specific purposes.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

obj.toLocaleString()
					

返回值

A string representing the object.

描述

Object 's toLocaleString returns the result of calling toString() .

This function is provided to give objects a generic toLocaleString method, even though not all may use it. See the list below.

Objects overriding toLocaleString

范例

Array toLocaleString() override

On Array objects, toLocaleString() can be used to print array values as a string, optionally with locale-specific identifiers (such as currency symbols) appended to them:

例如:

const testArray = [4, 7, 10];
let euroPrices = testArray.toLocaleString('fr', { style: 'currency', currency: 'EUR'});
// "4,00 €,7,00 €,10,00 €"
					

Date toLocaleString() override

On Date objects, toLocaleString() is used to print out date displays more suitable for specific locales:

例如:

const testDate = new Date(Date.now());
// "Date Fri May 29 2020 18:04:24 GMT+0100 (British Summer Time)"
let deDate = testDate.toLocaleString('de');
// "29.5.2020, 18:04:24"
var frDate = testDate.toLocaleString('fr');
//"29/05/2020 à 18:04:24"
					

Number toLocaleString() override

On Number objects, toLocaleString() is used to print out number displays more suitable for specific locales, e.g. with the correct separators:

例如:

const testNumber = 2901234564;
// "2901234564"
let deNumber = testNumber.toLocaleString('de');
// "2.901.234.564"
let frNumber = testNumber.toLocaleString('fr');
// "2 901 234 564"
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.prototype.toLocaleString' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
toLocaleString Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: