js常用小技巧
程序员文章站
2024-02-21 21:54:52
...
- string 类型的数字前面+,会转换成 number 类型,且去除数字开头所有的0
let a1 = '0000.12345'
let a2 = '.12345'
let a3 = ''
let a4 = '000000000000'
let a5 = '000000000100'
console.log(+a1) // 0.12345
console.log(+a2) // 0.12345
console.log(+a3) // 0
console.log(+a4) // 0
console.log(+a5) // 100
- 浮点数,无论 string 还是 numbe r类型,无论正负,直接截断小数部分,转为 number 类型
let a1 = '0.1111'
let a2 = '0.9999'
let a3 = '100.0001'
let a4 = '100.9999'
let a5 = 0.1111
let a6 = 0.9999
let a7 = 100.0001
let a8 = 100.9999
// 0 0 100 100 0 0 100 100
console.log(~~a1, ~~a2, ~~a3, ~~a4, ~~a5, ~~a6, ~~a7, ~~a8)
console.log(a1|0, a2|0, a3|0, a4|0 ,a5|0, a6|0, a7|0, a8|0)
继续更新补充!