【问题标题】:What is the relation between Number.prototype.toLocaleString() and Intl.NumberFormat.prototype.format?Number.prototype.toLocaleString() 和 Intl.NumberFormat.prototype.format 有什么关系?
【发布时间】:2017-02-06 16:24:21
【问题描述】:

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

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

【问题讨论】:

  • 感谢您的回复。是的,我阅读了它们,我可以看到通过这两种方式我可以完成几乎相似的任务。但是,我仍在尝试找出哪个更好(换句话说,我应该更喜欢哪个以及为什么)。我还在 localeString 文档中看到了对 Intl 命名空间的最小引用,但并没有完全掌握它们之间的关系。所以,寻找一些真实世界的例子。

标签: javascript localization number-formatting ecmascript-intl


【解决方案1】:

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

性能:

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

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

【讨论】:

  • Intl.NumberFormat 在您需要传递格式化程序函数时也很有用,例如在运行时根据配置文件或用户输入确定格式时。
猜你喜欢
  • 2016-09-28
  • 2013-07-02
  • 2022-01-03
  • 2020-03-05
  • 2012-09-27
  • 2020-04-17
  • 2016-03-21
  • 2015-07-08
  • 2018-12-31
相关资源
最近更新 更多