├── LICENSE ├── README.md ├── package.json └── src └── index.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 LiuXIn011 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

JavaScript的基础计算库

4 |

5 |

6 | 7 | 8 | 9 | 10 | 11 | 12 |

13 | 14 | 15 | | 计算方法 | 方法名 | 别名| 16 | |--|--|--| 17 | | 加法 | addition | jiafa,jia | 18 | | 减法 |subtraction |jianfa,jian | 19 | | 乘法 |multiplication | chengfa,cheng | 20 | | 除法 |division | chufa,chu | 21 | | 科学计数法 | scientificNotationToString |/ | 22 | 23 | ### 基本使用 24 | 25 | ```javascript 26 | // 全量引入 27 | import soeasymath from "soeasymath" 28 | // 按需引入 29 | import { subtraction } from "soeasymath" 30 | ``` 31 | ###### 加法 32 | ```javascript 33 | import { addition } from "soeasymath" 34 | console.log(addition(0.1,0.2,0.3....)) 35 | ``` 36 | ###### 减法 37 | ```javascript 38 | import { subtraction } from "soeasymath" 39 | console.log(subtraction(0.1,0.2,0.3....)) 40 | ``` 41 | ###### 乘法 42 | ```javascript 43 | import { multiplication } from "soeasymath" 44 | console.log(multiplication(0.1,0.2,0.3....)) 45 | ``` 46 | ###### 除法 47 | ```javascript 48 | import { division } from "soeasymath" 49 | console.log(division(0.1,0.2,0.3....)) 50 | ``` 51 | ###### 使用别名 52 | ```javascript 53 | import { jiafa } from "soeasymath" 54 | console.log(jiafa(0.1,0.2,0.3....)) 55 | ``` 56 | ###### 科学计数法 57 | ```javascript 58 | import { scientificNotationToString } from "soeasymath" 59 | console.log(scientificNotationToString(0.000000000000001)) 60 | ``` 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "soeasymath", 3 | "version": "2.0.1", 4 | "description": "soeasymath.js for Javascript", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "build" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/LiuXIn011/easyMath.git" 13 | }, 14 | "keywords": [ 15 | "javascript", 16 | "math" 17 | ], 18 | "author": "liuxin", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/LiuXIn011/easyMath/issues" 22 | }, 23 | "homepage": "https://github.com/LiuXIn011/easyMath#readme" 24 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | // 放大指定倍数取整 2 | const toInteger = function (number, times) { 3 | let maxDecimalDigits = 0 4 | // 如果是科学计数法 5 | if (String(number).indexOf("e") !== -1) { 6 | number = scientificNotationToString(number) 7 | } 8 | // 计算小数位数 9 | try { 10 | maxDecimalDigits = Math.max(maxDecimalDigits, String(number).split(".")[1].length) 11 | } catch (e) { 12 | maxDecimalDigits = 0 13 | } 14 | // 位数不足补0 15 | let mantissa = '' 16 | for (let i = 0; i < times - maxDecimalDigits; i++) { 17 | mantissa += '0' 18 | } 19 | // 返回结果 20 | if (maxDecimalDigits < times) { 21 | return Number(String(number).replace(".", "") + mantissa) 22 | } else { 23 | return Number(String(number).replace(".", "")) 24 | } 25 | } 26 | 27 | // 计算最长小数位数 28 | const getMaxDecimalDigits = function () { 29 | let maxDecimalDigits = 0 30 | const numberList = arguments[0] 31 | for (let i = 0; i < numberList.length; i++) { 32 | // 如果是科学计数法 33 | if (String(numberList[i]).indexOf("e") !== -1) { 34 | numberList[i] = scientificNotationToString(numberList[i]) 35 | } 36 | try { 37 | maxDecimalDigits = Math.max(maxDecimalDigits, String(numberList[i]).split(".")[1].length) 38 | } catch (e) { 39 | maxDecimalDigits = Math.max(maxDecimalDigits, 0) 40 | } 41 | } 42 | return maxDecimalDigits 43 | } 44 | // 科学计数法转字符串 45 | const scientificNotationToString = function (number) { 46 | let numberString = String(number) 47 | try { 48 | // 正整数 49 | if (numberString.indexOf("e+") !== -1) { 50 | let mantissaLength = Number(String(number).split("e+")[1]) 51 | // 组合0的个数 52 | let mantissa = '' 53 | for (let i = 0; i < mantissaLength; i++) { 54 | mantissa += '0' 55 | } 56 | return `${String(number).split("e+")[0]}${mantissa}` 57 | // 小数 58 | } else if (numberString.indexOf("e-") !== -1) { 59 | let mantissaLength = Number(String(number).split("e-")[1]) 60 | // 组合0的个数 61 | let mantissa = '' 62 | for (let i = 0; i < mantissaLength - 1; i++) { 63 | mantissa += '0' 64 | } 65 | return `0.${mantissa}${String(number).split("e-")[0].replace(".", "")}` 66 | } else { 67 | // 其他 68 | return numberString 69 | } 70 | } catch (error) { 71 | return numberString 72 | } 73 | } 74 | // 除法 75 | const division = function () { 76 | // 计算结果 77 | let maxDecimalDigits = getMaxDecimalDigits(arguments) 78 | let results = toInteger(arguments[0], maxDecimalDigits) 79 | for (let i = 1; i < arguments.length; i++) { 80 | results = results / toInteger(arguments[i], maxDecimalDigits) 81 | } 82 | // results = toInteger(results, arguments.length - 2) 83 | // 返回结果 84 | return results 85 | } 86 | // 除法别名 87 | const chufa = division 88 | const chu = division 89 | 90 | // 乘法 91 | const multiplication = function () { 92 | // 计算倍数 93 | let maxDecimalDigits = getMaxDecimalDigits(arguments) 94 | let times = Math.pow(10, maxDecimalDigits); 95 | // 计算结果 96 | let results = toInteger(arguments[0], maxDecimalDigits) 97 | for (let i = 1; i < arguments.length; i++) { 98 | results = results * toInteger(arguments[i], maxDecimalDigits) 99 | } 100 | results = results / Math.pow(times, arguments.length); 101 | // 返回结果 102 | return results 103 | } 104 | // 乘法别名 105 | const chengfa = multiplication 106 | const cheng = multiplication 107 | 108 | // 减法 109 | const subtraction = function () { 110 | // 计算倍数 111 | let maxDecimalDigits = getMaxDecimalDigits(arguments) 112 | let times = Math.pow(10, maxDecimalDigits); 113 | // 计算结果 114 | let results = toInteger(arguments[0], maxDecimalDigits) 115 | for (let i = 1; i < arguments.length; i++) { 116 | results -= toInteger(arguments[i], maxDecimalDigits) 117 | } 118 | results = results / times 119 | // 返回结果 120 | return results 121 | } 122 | // 减法别名 123 | const jianfa = subtraction 124 | const jian = subtraction 125 | 126 | // 加法 127 | const addition = function () { 128 | // 计算倍数 129 | let maxDecimalDigits = getMaxDecimalDigits(arguments) 130 | let times = Math.pow(10, maxDecimalDigits); 131 | // 计算结果 132 | let results = 0 133 | for (let i = 0; i < arguments.length; i++) { 134 | results += toInteger(arguments[i], maxDecimalDigits) 135 | } 136 | results = results / times 137 | // 返回结果 138 | return results 139 | 140 | } 141 | // 加法别名 142 | const jiafa = addition 143 | const jia = addition 144 | 145 | module.exports = { 146 | scientificNotationToString, 147 | division, 148 | chufa, 149 | chu, 150 | multiplication, 151 | chengfa, 152 | cheng, 153 | subtraction, 154 | jianfa, 155 | jian, 156 | addition, 157 | jiafa, 158 | jia, 159 | } --------------------------------------------------------------------------------