.toFixed()
let a = 0.33333
console.log(typeof a.toFixed(2)) // string
console.log(typeof 0.33333.toFixed(2)) // string
注意:经过.toFixed()处理后会改变number数据类型为string
let b = -0.33333
console.log(typeof b.toFixed(2)) // string
console.log(typeof (-0.33333).toFixed(2)) // string
console.log(typeof -0.33333.toFixed(2)) // number
注意:由于操作符优先级,负数不会返回string,还是number
toFixed()后,不改变number类型
let a = 0.33333
console.log(typeof +a.toFixed(2)) // number
console.log(typeof +0.33333.toFixed(2)) // number
本文深入探讨了JavaScript中toFixed()方法的使用,详细解释了该方法如何将数字转换为字符串,并保持指定的小数位数。特别关注了toFixed()对正负数的影响及数据类型的转换。

553

被折叠的 条评论
为什么被折叠?



