JavaScript Number.toLocaleString()

toLocaleString() 方法的语法是

num.toLocaleString(locales, options)

这里,num 是一个数字。


toLocaleString() 参数

toLocaleString() 方法接受

  • locales(可选)- 指定语言特定格式的字符串。
  • options(可选)- 带有配置属性的对象。

欲了解更多信息,请访问 Intl.NumberFormat() 构造函数


toLocaleString() 的返回值

  • 返回一个对给定数字进行语言敏感表示的字符串。

示例:使用 toLocaleString() 方法

let number = 400000;
console.log(number.toLocaleString()); // 400,000 if in US English locale

// using locales
let number1 = 123456.789;

// India uses thousands/lakh/crore separators
console.log(number1.toLocaleString("en-IN")); // 1,23,456.789

// using options
let currency = number1.toLocaleString("de-DE", {
  style: "currency",
  currency: "EUR",
  maximumSignificantDigits: 3,
});
console.log(currency); // 123.000 €

输出

400,000
1,23,456.789
123.000 €

推荐阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战