From 5cdafc9b6f91a5acafd1452a008dde0808dc56b8 Mon Sep 17 00:00:00 2001 From: Mondo Date: Fri, 15 Jan 2021 09:45:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=89=8D=E7=AB=AF=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bigJs/README.md | 26 ++++++++++++++++++++++++ bigJs/algor.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ bigJs/index.html | 26 ++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 bigJs/README.md create mode 100644 bigJs/algor.js create mode 100644 bigJs/index.html diff --git a/bigJs/README.md b/bigJs/README.md new file mode 100644 index 0000000..425cb16 --- /dev/null +++ b/bigJs/README.md @@ -0,0 +1,26 @@ +# 简单的计算 + +依赖 [bigJs](https://github.com/MikeMcl/big.js/) + +只做简单的封装 + +```html + + +``` + +```js +const $algor = new FedAlgor(); +console.log('加法: ', $algor.plus(5, 4)) // 9 +console.log('减法: ', $algor.minus('0.3', 0.1)) // 0.2 +console.log('乘法: ', $algor.times(4, 2)) // 8 +console.log('除法: ', $algor.div(1, 34, 7)) // 0.0294118 +console.log('toFixed: ', $algor.toFixed(0.555, 2)) // 0.56 + +// 添加自定义方法 +FedAlgor.prototype.mult = function(a, b) { + return 'mult' +} + +console.log($algor) +``` diff --git a/bigJs/algor.js b/bigJs/algor.js new file mode 100644 index 0000000..01b42f1 --- /dev/null +++ b/bigJs/algor.js @@ -0,0 +1,53 @@ +;(function(window) { + + function logWarn(res, len, callback) { + if (res.c) { + if (len) { + return callback(res, len) + } + return res.valueOf(); + } else { + console.warn(`结果不存在`); + } + } + + function FedAlgor() { + // 加法 + this.plus = function(a, b, len) { + var _a = new Big(+a); + var res = _a.plus(b); + return logWarn(res, len, this.toFixed); + } + // 减法 + this.minus = function(a, b, len) { + var _a = new Big(a); + var res = _a.minus(b); + return logWarn(res, len, this.toFixed); + } + // 乘法 + this.times = function(a, b, len) { + var _a = new Big(a); + var res = _a.times(b); + return logWarn(res, len, this.toFixed); + } + // 除法 + this.div = function(a, b, len) { + var _a = new Big(a); + var res = _a.div(b); + return logWarn(res, len, this.toFixed); + } + + // toFixed + this.toFixed = function(num, len = 2) { + var _num = new Big(num); + return _num.toFixed(len).valueOf(); + } + } + + if (typeof module === "object" && module && typeof module.exports === "object") { + module.exports = FedAlgor; + } else { + window.FedAlgor = window.__FedAlgor = FedAlgor; + } + +})(window) \ No newline at end of file diff --git a/bigJs/index.html b/bigJs/index.html new file mode 100644 index 0000000..964abcd --- /dev/null +++ b/bigJs/index.html @@ -0,0 +1,26 @@ + + + + + + 前端数值计算 + + + + + + + \ No newline at end of file