在 JavaScript 中,可以使用 toString() 方法将数字转换为字符串。例如:

let num = 123;
let str = num.toString(); // "123"

还可以使用 String() 函数将数字转换为字符串。例如:

let num = 123;
let str = String(num); // "123"

需要注意的是,使用 toString() 或 String() 将 null 或 undefined 转换为字符串时会返回 "null" 或 "undefined",而不是空字符串。如果需要将 null 或 undefined 转换为空字符串,可以使用以下代码:

let str = (num == null ? "" : num.toString());
js 数字转string

原文地址: https://www.cveoy.top/t/topic/crnj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录