Number.prototype.toLocaleString() 和 Intl.NumberFormat.prototype.format 有什么关系?

Typ*_*ter 4 javascript localization number-formatting ecmascript-intl

我们正在尝试在我们的应用程序中使用本地化支持并查看toLocaleString(). 我们还查看了Intl.NumberFormat它的格式化方法。

他们有什么关系吗?哪个更好用?

Mic*_*ski 7

来自 MDN 文档Number.prototype.toLocaleString()

表现:

格式化大量数字时,最好创建一个NumberFormat对象并使用其NumberFormat.format属性提供的功能。

在大多数情况下,您可以使用Number.prototype.toLocaleString(). 它使用起来更简单,因为它允许您使用一个函数调用来格式化数字。Intl.NumberFormat仅当您要格式化大量数字时才有用。