js浮点数的运算及比较

js浮点数的比较

  function MathFloatNumber(val1,val2,type){                     //val1:浮点数1      val2:浮点数2     type:运算类型(+,-,*,/,=)            let intnumber1=String(val1).split('.')[1].length                    //浮点数转string并获取小数点后面位数            let intnumber2=String(val2).split('.')[1].length        //浮点数转string并获取小数点后面位数            let biglength=Math.pow(10,(intnumber1>intnumber2?intnumber1:intnumber2))        //得到小数点位数多的数,计算浮点数转为整数的位数   (10的位数次方)            let bigval1=val1*biglength             //得到放大倍数的浮点数1            console.log(biglength)            let bigval2=val2*biglength           //得到放大倍数的浮点数2             switch(type){                 case 1:    //加法                     return (bigval1+bigval2)/biglength                /                       break                 case 2:    //减法                     return (bigval1-bigval2)/biglength                     break                 case 3:    //乘法                     return (bigval1*bigval2)/(biglength*biglength)                     break                   case 4:    //除法                     return (bigval1/bigval2).toFixed(3)                     break                                   case 5:                     return bigval1===bigval2                     break                 default:                     console.log('类型错误')             }         }         // var a=MathFloatNumber(0.1,0.05,2)         // console.log(a)         console.log(MathFloatNumber(MathFloatNumber(0.1,0.2,1),0.3,5))            
上一篇:ptootcms修改查询功能,例子:按条件查询出注册会员列表


下一篇:使用Hot Chocolate和.NET 6构建GraphQL应用(9) —— 实现Mutate更新数据