// 使用 toFixed 方法将数字格式化为指定小数位数的字符串
let num = 123.456;
// 将 num 格式化为保留两位小数的字符串
let result = num.toFixed(2);
console.log(result); // 输出 "123.46",注意:结果是四舍五入后的字符串
// 将 num 格式化为保留零位小数的字符串
result = num.toFixed(0);
console.log(result); // 输出 "123"
// 注意:toFixed 返回的是字符串类型,而不是数字类型
console.log(typeof result); // 输出 "string"
上一篇:js findindex
下一篇:js 对象赋值
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站