2.11 * 3.11 = 6.562099999999999;
// 取整
parseInt(2.11 * 3.11) // 6 (把数字或字符串转换成保留整数位的数字)
~~(2.11 * 3.11) // 6
2.11 * 3.11 | 0 // 6
// 转正确值
Number.prototype.just = function () {
const a = /\.\d+/.exec(this);
let b;
return a ? (b = /[^9\.]+/.exec(a[0]) || [''], Number(this.toFixed(b[0].length))) : 1 * this;
};
(2.11 * 3.11).just(); // 6.5621
(100.99999999999).just(); // 101
(100.111111).just(); // 100.111111
(100).just(); // 100
// 转String类型
(3).toString(); // "3"
(3).toString(2); // "11" (2进制, 即除尽2取余)
(3).toFixed(); // "3"
(3).toFixed(2); // "3.00" (四舍五入, 保留2位小数)
3 + ''; // "3"
// 转Number类型
Number('3.3'); // 3.3
isNaN(Number('a')); // NaN (字面量非数值的计算结果为NaN, 如 1*'a')
parseFloat('3.3'); // 3.3
'3.3' * 1; // 3.3
'3.3' - 0; // 3.3
'3.3' / 1; // 3.3