-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |