console.log(.1 + .2 === .3); // false
(10).toString(2); // '1010'
parseInt('1010', 2); // 10
除以 进制,取余,倒序
10 转 2
10 / 2 = 5, rem = 0
5 / 2 = 2, rem = 1
2 / 2 = 1, rem = 0
1 / 2 = 0, rem = 1
最终反着读: 1010
(0.1).toString(2)
'0.0001100110011001100110011001100110011001100110011001101'
乘以 进制,取整,正序
.1 * 2 = .2, rem = 0
.2 * 2 = .4, rem = 0
.4 * 2 = .8, rem = 0
.8 * 2 = .6, rem = 1
.6 * 2 = .2, rem = 1
.2 * 2 = .4, rem = 0
.4 * 2 = .8, rem = 0
.8 * 2 = .6, rem = 1
.6 * 2 = .2, rem = 1
最终:小数位不变 0. 后边是多少取多少: 0.0001100110011...
.1 + .2
// 0.30000000000000004
2.3 + 2.4
// 4.699999999999999
1.0 - .9
// 0.09999999999999998
3 * .3
// 0.8999999999999999
1.21 / 1.1
// 1.0999999999999999
作用:不四舍五入,只保留精度(从第一个不为0的位数开始算精度)
let numObj = 5.123456
console.log(numObj.toPrecision()) // logs '5.123456'
console.log(numObj.toPrecision(5)) // logs '5.1235'
console.log(numObj.toPrecision(2)) // logs '5.1'
console.log(numObj.toPrecision(1)) // logs '5'
console.log(numObj.toPrecision(20)) // logs '5.1234560000000000102'
console.log(numObj.toPrecision(19)) // logs '5.123456000000000010'
console.log(numObj.toPrecision(18)) // logs '5.12345600000000001'
console.log(numObj.toPrecision(17)) // logs '5.1234560000000000'
console.log(numObj.toPrecision(16)) // logs '5.123456000000000'
console.log((0.2).toPrecision(3)); // 0.200
作用:四舍五入,保留小数点位数
let numObj = 12345.6789
console.log(numObj.toFixed()); // log 12346
console.log(numObj.toFixed(1)); // log 12345.7
console.log(numObj.toFixed(6)); // 12345.678900
// 特殊情况
numObj = 0.105;
console.log(numObj.toFixed()); // log 0
console.log(numObj.toFixed(1)); // log 0.1
console.log(numObj.toFixed(2)); // log 0.10 (这里精度有问题,没有进一)
console.log(numObj.toFixed(6)); // log 0.105000
console.log(numObj.toFixed(16)); // log 0.1050000000000000
console.log(numObj.toFixed(17)); // log 0.10500000000000000
console.log(numObj.toFixed(18)); // log 0.104999999999999996(出现精度问题了)