博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 加减乘除以及四舍五入 新写法
阅读量:5132 次
发布时间:2019-06-13

本文共 3570 字,大约阅读时间需要 11 分钟。

1 四舍五入

eg: (1.23).round() = 1.2

(1.2456).round(3) = 1.246

Number.prototype.round = function (count) {    var res = 0;    var val = this.toString();    var num = parseInt(count || 2);    var dataArr = val.split('.');    var arr1 = dataArr[0] || "";    var arr2 = dataArr[1] || "";    var newArr2 = arr2;    if (arr2.length > num) {      var lastNum = arr2.substring(num, num + 1);      if (parseInt(lastNum) >= 5) {        newArr2 = parseInt(newArr2.substring(0, num)) + 1;        newArr2 = newArr2.toString();        if(newArr2.length
num) { arr1 = (parseInt(arr1) + 1).toString(); newArr2 = newArr2.substring(1, 3); } } } val = parseFloat(arr1 + '.' + newArr2); res = parseFloat(val.toFixed(num)); return res; }

 

2 加减乘除

eg: (1).plus(2) = 3

if (!Number.prototype.plus) { //'+'    Number.prototype.plus = function (addend) {        var arg1 = this,            arg2 = addend;        var r1,            r2,            m,            num1,            num2;        try {            r1 = arg1.toString().split(".")[1].length;        } catch (e) {            r1 = 0        }        try {            r2 = arg2.toString().split(".")[1].length;        } catch (e) {            r2 = 0        }        m = Math.pow(10, Math.max(r1, r2));        num1 = r1 == 0 ? parseInt(arg1 * m) : (parseInt(`${arg1.toString().replace('.', '')}`) * m / (Math.pow(10, r1)));        num2 = r2 == 0 ? parseInt(arg2 * m) : (parseInt(`${arg2.toString().replace('.', '')}`) * m / (Math.pow(10, r2)));        return (num1 + num2) / m;    }}if (!Number.prototype.minus) { //'-'    Number.prototype.minus = function (Subtraction) {        var arg1 = this,            arg2 = Subtraction;        var r1,            r2,            m,            n,            num1,            num2;        try {            r1 = arg1.toString().split(".")[1].length        } catch (e) {            r1 = 0        }        try {            r2 = arg2.toString().split(".")[1].length        } catch (e) {            r2 = 0        }        m = Math.pow(10, Math.max(r1, r2));        n = (r1 >= r2) ? r1 : r2;        num1 = r1 == 0 ? parseInt(arg1 * m) : (parseInt(`${arg1.toString().replace('.', '')}`) * m / (Math.pow(10, r1)));        num2 = r2 == 0 ? parseInt(arg2 * m) : (parseInt(`${arg2.toString().replace('.', '')}`) * m / (Math.pow(10, r2)));        return parseFloat(((num1 - num2) / m).toFixed(n));    }}if (!Number.prototype.multiply) { //'*'    Number.prototype.multiply = function (multiple) {        var arg1 = this,            arg2 = multiple;        var m = 0,            s1 = arg1.toString(),            s2 = arg2.toString();        try {            m += s1.split(".")[1].length        } catch (e) { }        try {            m += s2.split(".")[1].length        } catch (e) { }        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)    }    if (!Number.prototype.devide) { //'/'        Number.prototype.devide = function (divisor) {            var arg1 = this,                arg2 = divisor;            var t1 = 0,                t2 = 0,                r1,                r2;            try {                t1 = arg1.toString().split(".")[1].length            } catch (e) { }            try {                t2 = arg2.toString().split(".")[1].length            } catch (e) { }            r1 = Number(arg1.toString().replace(".", ""))            r2 = Number(arg2.toString().replace(".", ""))            return (r1 / r2).multiply(Math.pow(10, t2 - t1));        }    }}

 

转载于:https://www.cnblogs.com/zxhome/p/10342313.html

你可能感兴趣的文章
alter database databasename set single_user with rollback IMMEDIATE 不成功问题
查看>>
【题解】青蛙的约会
查看>>
autopep8
查看>>
GIT在Linux上的安装和使用简介
查看>>
Android 官方新手指导教程
查看>>
幸运转盘v1.0 【附视频】我的Android原创处女作,请支持!
查看>>
[51nod] 1199 Money out of Thin Air #线段树+DFS序
查看>>
Red and Black(poj-1979)
查看>>
安装 Express
查看>>
存储(硬件方面的一些基本术语)
查看>>
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>
大家在做.NET B/S项目的时候多用什么设技术啊?
查看>>
Java SE和Java EE应用的性能调优
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>