数字加减问题解决精度问题
Published:
·
LastMod: November 18, 2021
·
54 words
1
2
3
4
5
6
7
8
9
10
11
|
function plus(num1, num2) {
const num1Digits = (num1.toString().split('.')[1] || '').length;
const num2Digits = (num2.toString().split('.')[1] || '').length;
const baseNumber = Math.pow(10, Math.max(num1Digits, num2Digits));
return (num1 * baseNumber + num2 * baseNumber) / baseNumber;
}
function minus(num1, num2) {
return plus(num1, -1 * num2);
}
|