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