1.相减的方法:
function subtractDecimal(a, b) {
const [aInt, aDec] = a.split('.');
const [bInt, bDec] = b.split('.');
const intDiff = Number(aInt) - Number(bInt);
const decDiff = Number(`0.${aDec || '0'}`) - Number(`0.${bDec || '0'}`);
return (intDiff + decDiff).toFixed(Math.max((aDec || '').length, (bDec || '').length));
}
2.相加的方法
function addDecimal(a, b) {
const [aInt, aDec] = a.split('.');
const [bInt, bDec] = b.split('.');
const intSum = Number(aInt) + Number(bInt);
const decSum = Number(`0.${aDec || '0'}`) + Number(`0.${bDec || '0'}`);
return (intSum + decSum).toFixed(Math.max((aDec || '').length, (bDec || '').length));
}