js保留两位小数
此方法会改变数据类型变为string,但是不会四舍五入,用的是字符串截取,不够两位小数补零
let value = 5.53 let numStr = Number(value).toString() let index = numStr.indexOf('.') let transformVal = "" if (index == '-1') { transformVal = numStr + ".00" } else { let numbers = (numStr.slice(index + 1)).length switch (numbers) { case 1: transformVal = numStr + '0' break; case 2: transformVal = numStr break; default: transformVal = numStr.slice(0, index + 3) break; } } console.log(transformVal);