//num1 num2传入两个值 symbol +-*/符号 function amend(num1,num2,symbol){ var str1=num1.toString(),str2=num2.toString(),result,str1Length,str2Length //解决整数没有小数点方法 try {str1Length= str1.split('.')[1].length} catch (error) {str1Length=0} try {str2Length= str2.split('.')[1].length} catch (error) {str2Length=0} var step=Math.pow(10,Math.max(str1Length,str2Length)) // console.log(step); switch (symbol) { case "+": result= (num1*step+num2*step)/step break; case "-": result= (num1*step-num2*step)/step break; case "*": result= ((num1*step)*(num2*step)) / step/step break; case "/": result= (num1*step)/(num2*step) break; default: break; } return result }
标签:num1,num2,str2Length,js,break,step,result,精度,小数 From: https://www.cnblogs.com/wangshengli520/p/16981229.html