首页 > 其他分享 >处理js小数精度计算问题

处理js小数精度计算问题

时间:2022-12-14 09:22:26浏览次数:112  
标签:num1 num2 str2Length js break step result 精度 小数

//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

相关文章

  • Python中json.dump()和json.dumps()的区别
    一、图解json.dumps(dict,indent):将Python对象转换成json字符串 json.dump(dict,file_pointer):将Python对象写入json文件  二、json.dumps()用法1、用法json.d......
  • 卸载 nodejs 时报错 you must be an administrator to remove this application 的解
    这是在win11下报的错,要不怎么说win11就是个笑话。我是这么解决的:首先找到原始的安装包。再以管理员身份打开cmd(怎么操作?先打开一个普通的cmd,然后按住ctl+shift......
  • SAP根据源码导入/ui2/cl_json类
    之前我都是用CALLTRANSFORMATIONid方式来解析json数据的,结果发现解析出来的的数据有问题。无奈之下只好使用/ui2/cl_json类方法了,结果发现SAP版本不够,没有这个方法,网上......
  • Proj4.js使用初步入门
    一、基础知识在正式使用Proj4.js时,我们首先需要了解下有关各种投影的参数定义(例如:椭球长半轴、扁率、原点纬线、中央经线、两条标准纬线、东偏移量、北偏移量和单位等),具......
  • mojs——在Vue中使用mojs
    前言一个需求需要做动画效果,刚好搏皮有用mojs,就打算通过mojs实现;Vue中的$nextTick有什么作用:Vue中的$nextTick有什么作用?mojs:https://mojs.github.io/tutorials/内......
  • 【JS】ES5 语法实现继承
    构造函数AfunctionA(){}构造函数BfunctionB(){A.call(this);}修改B的原型对象B.prototype=newA();//contructor指向原来的构造函数BB.......
  • springMVC06(1-响应,2-类返回成JSON数据)
    一、大纲二、响应JSON数据(把你给的"类"转化成"JSON"数据)2.1:需要有"@ResponseBody"这个注解2.2:需要导入JSON坐标<dependency><groupId>com.fasterxml.......
  • 【JS】原型链
    构造函数AfunctionA(){}实例对象avara=newA();原型对象prototype和对象原型protoa.__proto__===A.prototype;//trueconstructor属性a.__pro......
  • 对JSON里的字段进行排序
     publicstaticstringStortJson(stringjson){vardic=JsonConvert.DeserializeObject<SortedDictionary<string,object>>(json);SortedDictionary<strin......
  • #yyds干货盘点#JS数据类型判断几种方式
     一般JS检测数据类型有4种方法:typeof、constructor、instanceof和Object.prototype.toString,相信大家也对这几种判断很熟悉,下面我再罗列两种,供各位使用。​typeof:检测基......