Math对象

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Math对象</title>
</head>

<body>

</body>
<script>
    console.log("1.random方法:", Math.random()) //返回一个0~1之间的随机浮点数,包括0不包括1
    console.log("2.min方法:", Math.min(12, 33, 66)) //接受多个参数,返回最小值
    console.log("3.max方法:", Math.max(12, 33, 66)) //接受多个参数,返回最小值

    console.log("4.round方法-四舍五入:", Math.round(3.5)) //四舍五入
    console.log("5.floor方法-向下取整:", Math.floor(3.5)) //向下取整
    console.log("6.ceil方法-向上取整:", Math.ceil(3.5)) //向上取整

    // import math from './4.1math.js'

    console.log("7.加:", add(3, 4))
    console.log("8.减:",subtract(3, 4))
    console.log("9.乘:",multiply(5, 4))
    console.log("10.除:",divide(36, 6))
    function add(a, b) {
        let c, d, e;

        try {
            c = a.toString().split('.')[1].length;
        } catch (f) {
            c = 0;
        }

        try {
            d = b.toString().split('.')[1].length;
        } catch (f) {
            d = 0;
        }

        e = Math.pow(10, Math.max(c, d));

        return (multiply(a, e) + multiply(b, e)) / e;
    }

    function subtract(a, b) {
        let c, d, e;

        try {
            c = a.toString().split('.')[1].length;
        } catch (f) {
            c = 0;
        }

        try {
            d = b.toString().split('.')[1].length;
        } catch (f) {
            d = 0;
        }

        e = Math.pow(10, Math.max(c, d));

        return (multiply(a, e) - multiply(b, e)) / e;
    }

    function multiply(a, b) {
        let c = 0,
            d = a.toString(),
            e = b.toString();

        try {
            c += d.split('.')[1].length;
        } catch (f) {} // eslint-disable-line no-empty

        try {
            c += e.split('.')[1].length;
        } catch (f) {} // eslint-disable-line no-empty

        return (
            Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
        );
    }

    function divide(a, b) {
        let c,
            d,
            e = 0,
            f = 0;

        try {
            e = a.toString().split('.')[1].length;
        } catch (g) {} // eslint-disable-line no-empty

        try {
            f = b.toString().split('.')[1].length;
        } catch (g) {} // eslint-disable-line no-empty

        c = Number(a.toString().replace('.', ''));
        d = Number(b.toString().replace('.', ''));

        return (c / d) * Math.pow(10, f - e);
    }

   

   console.log("11. 格式化小数:", numberToFixed(3.000255, 4))
   console.log("12. numFix:", numFix(3.100001111, 3))
   // 格式化小数,会补齐小数位,比如1.11格式化为4位就是1.1100
   function numberToFixed(num, n){
      if(!num) return 0;
      let r = num - 0;
      r = formatScienceNum(r);
      let t =numFix(r, n);
      let l = 0;
      if(t.indexOf('.') != -1) {
        let x = t.split('.')[1];
        if(x) {
          l = n - x.length;
        }
      } else {
        l = n;
        if(n > 0) t += '.';
      }
      if(l > 0) {
        for(var i = 0; i < l; i ++) {
          t += '0';
        }
      }
      return (isNaN(t) || t == 'NaN') ? '' : t;
    }
    //将科学记数法的数字正常显示
    function formatScienceNum(num) {
      let n = Math.abs(num);
      let r = n + '';
      if((r.toLowerCase().indexOf('e') >= 0) && (n < 1)) {
        r = '0' + (Number(r) + 1 + '').substring(1);
      }
      if(num < 0) r = '-' + r;
      return r;
    }
    // 截取小数
    function numFix(n, r) {
      let re = n + '';
      re = re.replace(/[^\d\.]/g, '');
      if(re) {
        if(re.indexOf('.') != (-1)) {
          let tempRe = re.split('.');
          let z = tempRe[0];
          let x = '';
          if(tempRe.length > 1) {
            x = tempRe[1].substring(0, r);
          }
          if(r > 0) {
            tempRe = z + '.' + x;
          } else {
            tempRe = z;
          }
          re = tempRe;
        }
      }
      return re;
    }
</script>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。