calculate1plus1 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +34 -0
- package/package.json +11 -0
package/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 计算两个数字的和
|
|
3
|
+
* @param {number} a - 第一个数字
|
|
4
|
+
* @param {number} b - 第二个数字
|
|
5
|
+
* @returns {number} 两个数字的和
|
|
6
|
+
* @example
|
|
7
|
+
* const calculateSum = require('my-calculateSum-package');
|
|
8
|
+
* console.log(calculateSum(2, 3)); // 输出: 5
|
|
9
|
+
*/
|
|
10
|
+
function calculateSum(a, b) {
|
|
11
|
+
// 输入验证
|
|
12
|
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
13
|
+
throw new TypeError('两个参数都必须是数字');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 处理浮点数精度问题
|
|
17
|
+
const precision = Math.max(
|
|
18
|
+
(a.toString().split('.')[1] || '').length,
|
|
19
|
+
(b.toString().split('.')[1] || '').length
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const multiplier = Math.pow(10, precision);
|
|
23
|
+
return (Math.round(a * multiplier) + Math.round(b * multiplier)) / multiplier;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 导出函数
|
|
27
|
+
module.exports = calculateSum;
|
|
28
|
+
|
|
29
|
+
// 也可以导出为 ES6 模块格式(可选)
|
|
30
|
+
if (typeof module !== 'undefined' && module.exports && typeof exports !== 'undefined') {
|
|
31
|
+
exports.default = calculateSum;
|
|
32
|
+
module.exports = calculateSum;
|
|
33
|
+
exports = module.exports = calculateSum;
|
|
34
|
+
}
|