Skip to content

Commit

Permalink
feat: 前端计算值
Browse files Browse the repository at this point in the history
  • Loading branch information
imondo committed Jan 15, 2021
1 parent 69b8785 commit 5cdafc9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bigJs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 简单的计算

依赖 [bigJs](https://github.com/MikeMcl/big.js/)

只做简单的封装

```html
<script src='https://cdn.jsdelivr.net/npm/[email protected]/big.min.js'></script>
<script src="./algor.js"></script>
```

```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)
```
53 changes: 53 additions & 0 deletions bigJs/algor.js
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions bigJs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>前端数值计算</title>
</head>
<body>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/big.min.js'></script>
<script src="./algor.js"></script>
<script>
const $algor = new FedAlgor();
console.log('加法: ', $algor.plus(5, 4))
console.log('减法: ', $algor.minus('0.3', 0.1))
console.log('乘法: ', $algor.times(4, 2))
console.log('除法: ', $algor.div(1, 34, 7))
console.log('toFixed: ', $algor.toFixed(0.555, 2))

// 添加自定义方法
FedAlgor.prototype.mult = function(a, b) {
console.log(11);
return 'mult'
}
</script>
</body>
</html>

0 comments on commit 5cdafc9

Please sign in to comment.