移动微网站建设,做民宿需要和多家网站合作吗,沈阳网页制作设计营销,网页版qq浏览器登录入口方法一#xff1a; 保留字符串类型#xff0c;传给后端
方法二#xff1a; 如果涉及到计算#xff0c;用以下方法
// 核心思想 在计算前#xff0c;将数字乘以相同倍数#xff0c;让他没有小数位#xff0c;然后再进行计算#xff0c;然后再除以相同的倍数#xff0…方法一 保留字符串类型传给后端
方法二 如果涉及到计算用以下方法
// 核心思想 在计算前将数字乘以相同倍数让他没有小数位然后再进行计算然后再除以相同的倍数恢复原来的小数位
function add(a, b) {const precisionA getPrecisionA(a)const precisionB getPrecisionB(b)var multiplier Math.pow(10, Math.max(precisionA, precisionB))return (a * multiplier b * multiplier) / multiplier
}function getPrecisionA() {if (num.toString().indexOf(e) ! -1) {var precision num.toString().split(e-)[1]return parseInt(precision, 10)}var decimalPart num.toString().split(.)[1]return decimalPart ? decimalPart.length : 0
}以下是完整函数
/*** 把递归操作扁平迭代化* param {number[]} arr 要操作的数字数组* param {function} operation 迭代操作* private*/
function iteratorOperation(arr,operation){
const [num1,num2,...others]arr;
let resoperation(num1,num2)
other.forEach((num){
resoperation(res,num)
})
return res
}/*** 返回小数部分的长度* private* param {*number} num Input number*/function digitLength(num){// 指数部分通常是用字母 e 或 E 后跟一个整数表示const eSplitnum.toString().spilt(/[eE]/);const len(eSplit[0].split(.)[1] || ).length - (eSplit[1] || 0);return len0?len:0;
}/*** 把小数转成整数,如果是小数则放大成整数* private* param {*number} num 输入数*/
function float2Fixed(num){
if(num.toString().indexOf(e)-1){
return Number(num.toString().replace(.,))
}
const dLen digitLength(num);return dLen 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
}/*** 把错误的数据转正* private* example strip(0.09999999999999998)0.1*/function strip(num,precision15){// toPrecision将数字转换为具有指定有效数字位数的字符串表示形式// parseFloat 将字符串转为浮点数return parseFloat(Number(num).toPrecision(precision));}/*** 高精度乘法*/function times(...nums){if (nums.length 2) {return iteratorOperation(nums, times);}const [num1, num2] nums;const num1Changed float2Fixed(num1);const num2Changed float2Fixed(num2);const baseNum digitLength(num1) digitLength(num2);const leftValuenum1Changed * num2Changed// 并返回底数的指数次幂return leftValue/ Math.pow(10, baseNum);}/*** 高精度加法*/function plus(...nums){
if(nums.length2){
return iteratorOperation(nums, plus)
}
const [num1,num2]nums
// 取最大的小数位
// Math.pow 函数计算以 10 为底、指数为两个数字小数部分长度的最大值的幂次方
const baseNumMath.pow(10,Math.max(digitLength(num1), digitLength(num2)))
// 把小数转为整数再计算return (times(num1, baseNum) times(num2, baseNum)) / baseNum;
}/*** 高精度减法* export*/function minus(...nums) {if (nums.length 2) {return iteratorOperation(nums, minus);}const [num1, num2] nums;const baseNum Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
}/*** 高精度除法* export*/
function divide(...nums) {if (nums.length 2) {return iteratorOperation(nums, divide);}const [num1, num2] nums;const num1Changed float2Fixed(num1);const num2Changed float2Fixed(num2);checkBoundary(num1Changed);checkBoundary(num2Changed);// 重要这里必须用strip进行修正return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
}
文章转载自: http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.qpzjh.cn.gov.cn.qpzjh.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.pcqxr.cn.gov.cn.pcqxr.cn http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.dwncg.cn.gov.cn.dwncg.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn