Number.prototype.toLocaleString()
toLocaleString() 方法是 Number 对象的一个方法,它返回一个表示该数字的、符合语言习惯的字符串。在支持 Intl.NumberFormat API 的实现中,此方法会委托给 Intl.NumberFormat。
每次调用 toLocaleString 时,都需要在一个庞大的本地化字符串数据库中进行搜索,这可能会效率低下。当使用相同的参数多次调用该方法时,最好创建一个 Intl.NumberFormat 对象并使用其 format() 方法,因为 NumberFormat 对象会记住传递给它的参数,并可能决定缓存数据库的一部分,从而使将来的 format 调用可以在更受限的上下文中搜索本地化字符串。
试一试
function eArabic(x) {
return x.toLocaleString("ar-EG");
}
console.log(eArabic(123456.789));
// Expected output: "١٢٣٬٤٥٦٫٧٨٩"
console.log(eArabic("123456.789"));
// Expected output: "123456.789"
console.log(eArabic(NaN));
// Expected output: "ليس رقم"
语法
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
参数
locales 和 options 参数自定义函数行为,并允许应用程序指定应使用的语言的格式约定。
在支持 Intl.NumberFormat API 的实现中,这些参数与 Intl.NumberFormat() 构造函数的参数完全对应。不支持 Intl.NumberFormat 的实现被要求忽略这两个参数,因此所使用的区域设置和返回字符串的格式完全取决于实现。
locales可选-
一个字符串,包含一个 BCP 47 语言标签,或者这些字符串的数组。对应于
Intl.NumberFormat()构造函数的locales参数。在不支持
Intl.NumberFormat的实现中,此参数被忽略,通常会使用主机的区域设置。 options可选-
一个调整输出格式的对象。对应于
Intl.NumberFormat()构造函数的options参数。在不支持
Intl.NumberFormat的实现中,此参数被忽略。
有关这些参数的详细信息以及如何使用它们,请参阅 Intl.NumberFormat() 构造函数。
返回值
一个字符串,根据特定语言的约定表示给定的数字。
在支持 Intl.NumberFormat 的实现中,这等同于 new Intl.NumberFormat(locales, options).format(number)。
注意: 大多数情况下,toLocaleString() 返回的格式是一致的。然而,输出在不同实现之间可能会有所不同,即使在相同的区域设置中也是如此——输出差异是设计使然,并受规范允许。它也可能不符合您的预期。例如,字符串可能使用不间断空格或被双向控制字符包围。您不应将 toLocaleString() 的结果与硬编码常量进行比较。
示例
使用 toLocaleString()
不指定 locale 的此方法的简单用法以默认区域设置和默认选项返回格式化字符串。
const number = 3500;
console.log(number.toLocaleString()); // "3,500" if in U.S. English locale
检查对 locales 和 options 参数的支持
locales 和 options 参数可能并非在所有实现中都得到支持,因为对国际化 API 的支持是可选的,并且某些系统可能没有必要的数据。对于不支持国际化的实现,toLocaleString() 始终使用系统的语言环境,这可能不是您想要的。由于任何支持 locales 和 options 参数的实现都必须支持 Intl API,因此您可以检查后者是否存在来判断是否支持。
function toLocaleStringSupportsLocales() {
return (
typeof Intl === "object" &&
!!Intl &&
typeof Intl.NumberFormat === "function"
);
}
使用语言环境
此示例展示了本地化数字格式的一些变化。为了获得应用程序用户界面中使用的语言的格式,请务必使用 locales 参数指定该语言(以及可能的备用语言)。
const number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString("de-DE"));
// 123.456,789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString("ar-EG"));
// ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(number.toLocaleString("en-IN"));
// 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// 一二三,四五六.七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(["ban", "id"]));
// 123.456,789
使用选项
toLocaleString() 提供的结果可以使用 options 参数进行自定义。
const number = 123456.789;
// request a currency format
console.log(
number.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(
number.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }),
);
// ¥123,457
// limit to three significant digits
console.log(number.toLocaleString("en-IN", { maximumSignificantDigits: 3 }));
// 1,23,000
// Use the host default language with options for number formatting
const num = 30000.65;
console.log(
num.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
);
// "30,000.65" where English is the default language, or
// "30.000,65" where German is the default language, or
// "30 000,65" where French is the default language
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-number.prototype.tolocalestring |
| ECMAScript® 2026 国际化 API 规范 # sup-number.prototype.tolocalestring |
浏览器兼容性
加载中…