JavaScript Object.prototype.toLocaleString() 方法

Object.prototype.toLocaleString() 语法

toLocaleString() 是 JavaScript Object 对象实例的一个方法,它用于返回当前对象的字符串表示,该字符串会根据语言环境(locale)的约定来进行格式化。

语法:

obj.toLocaleString(locales, options)

说明:

toLocaleString() 方法接收以下 2 个参数。

  • locales(可选):是一个字符串或一个字符串数组,表示用于格式化的语言环境。例如,"en-US" 表示美式英语,"zh-CN" 表示简体中文。如果省略此参数,则表示使用运行环境的默认语言环境。
  • options(可选):是一个对象,用于配置格式化的选项。这些选项会因调用 toLocaleString() 的具体内置对象(如 Number、Date)而异。

JavaScript 所有内置对象(如 Object、String、Array、Number、Boolean 等)都有 toLocaleString() 方法。对于其他内置对象的 toLocaleString() 方法,另请参阅:

提示:

  • Object.prototype.toLocaleString() 是 Object 原型链上的方法。也就是说,所有继承自 Object.prototype 的对象都默认拥有这个方法,并且可以通过对象实例直接调用(如 obj.toLocaleString())。
  • 在格式化数字或日期时,直接调用 Number.prototype.toLocaleString() 或 Date.prototype.toLocaleString() 通常比 Object.prototype.toLocaleString() 的默认行为更有效和灵活,因为它们提供了更丰富的 options 参数。

Object.prototype.toLocaleString() 摘要

属于 JavaScript Object 对象
使用频率
官方文档 查看
MDN 查看

Object.prototype.toLocaleString() 示例

接下来,我们通过几个简单的例子来讲解一下 toLocaleString() 方法是如何使用的。

示例 1:toLocaleString() vs toString()

const obj = {
    name: "Jack",
    age: 20
};

console.log(obj.toString());
console.log(obj.toLocaleString());

运行结果如下。

[object Object]
[object Object]

分析:

对于普通对象(即 Object 对象)来说,toLocaleString() 和 toString() 的返回值是相同的。

示例 2:Number.prototype.toLocaleString() 用于格式化数字

const price = 314.159;

// 默认语言环境格式化 (取决于运行环境)
console.log(price.toLocaleString());

// 美式英语格式化 (千位分隔符)
console.log(price.toLocaleString("en-US"));

// 中文 (简体) 格式化 (千位分隔符)
console.log(price.toLocaleString("zh-CN"));

// 货币格式化 (欧元)
console.log(price.toLocaleString("de-DE", { style: "currency", currency: "EUR" }));

运行结果如下。

314.159
314.159
314.159
314,16

分析:

对于 Number.prototype.toLocaleString() 来说,我们可以通过传入 locales 和 options 参数,来精确控制数字的千位分隔符、货币符号、小数位数等的表示。

示例 3:Date.prototype.toLocaleString() 用于格式化日期时间

const time = new Date();

// 默认语言环境格式化 (取决于运行环境)
console.log(time.toLocaleString());

// 美式英语日期格式
console.log(time.toLocaleString("en-US", { year: "numeric", month: "long", day: "numeric" }));

// 德语日期和时间格式
console.log(time.toLocaleString("de-DE", { dateStyle: "full", timeStyle: "medium" }));

// 带有缩写时区的中文时间格式
console.log(time.toLocaleString("zh-CN", { hour: "2-digit", minute: "2-digit", second: "2-digit", timeZoneName: "short" }));

运行结果如下。

2025/6/14 07:15:55
June 14, 2025
Samstag, 14. Juni 2025 um 07:15:55
GMT+8 07:15:55

分析:

Date.prototype.toLocaleString() 提供了强大的日期和时间格式化能力,我们可以控制日期部分的显示方式、时间部分的显示方式以及时区等。

示例 4:Array.prototype.toLocaleString() 用于格式化数组元素

const nums = [1000, 2000, 3000];
// 默认语言环境
console.log(nums.toLocaleString());
// 德语环境格
console.log(nums.toLocaleString("de-DE"));

const dates = [new Date("2025-01-01"), new Date("2025-05-20")];
// 默认语言环境
console.log(dates.toLocaleString());
// 特定语言环境和选项
console.log(dates.toLocaleString("zh-CN", { year: "numeric", month: "numeric", day: "numeric" }));

运行结果如下。

1,000,2,000,3,000
1.000,2.000,3.000
1/1/2025, 08:00:00 AM,5/20/2025, 08:00:00 AM
2025/1/1,2025/5/20

分析:

Array.prototype.toLocaleString() 用于将数组的每个元素转换为字符串,并使用语言环境特定的分隔符连接。

给站长反馈

绿叶网正在不断完善中,小伙伴们如果发现任何问题,还望多多给站长反馈,谢谢!

邮箱:lvyenet@vip.qq.com

「绿叶网」服务号
绿叶网服务号放大
关注服务号,微信也能看教程。
绿叶网服务号