function multiply (a, b) { let sa = a.toString(); let sb = b.toString(); let ia = sa.lastIndexOf('.') != -1 ? sa.length - 1 - sa.lastIndexOf('.') : 0; let ib = sb.lastIndexOf('.') != -1 ? sb.length - 1 - sb.lastIndexOf('.') : 0; let max = Math.max(ia, ib); return (a * b).toFixed(max); }
function multiply(a, b) { let aIndex = a.toString().replace(".", "").length - 1; let bIndex = b.toString().replace(".", "").length - 1; let aData = a * Math.pow(10, aIndex); let bData = b * Math.pow(10, bIndex); let divide = Math.pow(10, aIndex + bIndex); return (aData * bData) / divide; }
function multiply(a, b) { if (Number.isInteger(a) && Number.isInteger(b)) { return a * b } if (!Number.isInteger(a) || !Number.isInteger(b)) { const args = [a, b]; return args.reduce((sum, cur) => { if (Number.isInteger(cur)) { sum *= cur return sum } else { const numArr = cur.toString().split('.'); const bArr = numArr[1].split(''); const bindex = bArr.findIndex((item) => item > 0); const curNumber = +bArr.slice(bindex, bArr.length).join('').toString(); const tens = Math.pow(10, numArr[1].length) cur = curNumber sum *= cur sum = sum / tens return sum } }, 1) } // 小数点 function changeNumber(...args) { return args.reduce((sum, cur) => { if (Number.isInteger(cur)) { sum *= cur return sum } else { const numArr = cur.toString().split('.'); const bArr = numArr[1].split(''); const bindex = bArr.findIndex((item) => item > 0); const curNumber = +bArr.slice(bindex, bArr.length).join('').toString(); const tens = Math.pow(10, numArr[1].length) cur = curNumber sum *= cur sum = sum / tens return sum } }, 1) } }
function multiply(a, b) { var a1=a*10000; var b1=b*10000; var temp = a1*b1; var result = temp/100000000; return result }比较野蛮的方法 大家应该一看就懂