@kkkf/utils 0.0.1
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/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +67 -0
- package/dist/index.mjs +37 -0
- package/package.json +10 -0
- package/src/index.ts +1 -0
- package/src/math.ts +38 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +8 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 精确加法 */
|
|
2
|
+
declare function add(a: number, b: number): number;
|
|
3
|
+
/** 精确减法 */
|
|
4
|
+
declare function subtract(a: number, b: number): number;
|
|
5
|
+
/** 精确乘法 */
|
|
6
|
+
declare function multiply(a: number, b: number): number;
|
|
7
|
+
/** 精确除法 */
|
|
8
|
+
declare function divide(a: number, b: number): number;
|
|
9
|
+
|
|
10
|
+
export { add, divide, multiply, subtract };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 精确加法 */
|
|
2
|
+
declare function add(a: number, b: number): number;
|
|
3
|
+
/** 精确减法 */
|
|
4
|
+
declare function subtract(a: number, b: number): number;
|
|
5
|
+
/** 精确乘法 */
|
|
6
|
+
declare function multiply(a: number, b: number): number;
|
|
7
|
+
/** 精确除法 */
|
|
8
|
+
declare function divide(a: number, b: number): number;
|
|
9
|
+
|
|
10
|
+
export { add, divide, multiply, subtract };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
add: () => add,
|
|
24
|
+
divide: () => divide,
|
|
25
|
+
multiply: () => multiply,
|
|
26
|
+
subtract: () => subtract
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/math.ts
|
|
31
|
+
function getDecimalLen(num) {
|
|
32
|
+
const str = String(num);
|
|
33
|
+
const index = str.indexOf(".");
|
|
34
|
+
return index === -1 ? 0 : str.length - index - 1;
|
|
35
|
+
}
|
|
36
|
+
function add(a, b) {
|
|
37
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b));
|
|
38
|
+
const base = Math.pow(10, maxLen);
|
|
39
|
+
return (Math.round(a * base) + Math.round(b * base)) / base;
|
|
40
|
+
}
|
|
41
|
+
function subtract(a, b) {
|
|
42
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b));
|
|
43
|
+
const base = Math.pow(10, maxLen);
|
|
44
|
+
return (Math.round(a * base) - Math.round(b * base)) / base;
|
|
45
|
+
}
|
|
46
|
+
function multiply(a, b) {
|
|
47
|
+
const lenA = getDecimalLen(a);
|
|
48
|
+
const lenB = getDecimalLen(b);
|
|
49
|
+
const intA = Number(String(a).replace(".", ""));
|
|
50
|
+
const intB = Number(String(b).replace(".", ""));
|
|
51
|
+
return intA * intB / Math.pow(10, lenA + lenB);
|
|
52
|
+
}
|
|
53
|
+
function divide(a, b) {
|
|
54
|
+
if (b === 0) throw new Error("\u9664\u6570\u4E0D\u80FD\u4E3A 0");
|
|
55
|
+
const lenA = getDecimalLen(a);
|
|
56
|
+
const lenB = getDecimalLen(b);
|
|
57
|
+
const intA = Number(String(a).replace(".", ""));
|
|
58
|
+
const intB = Number(String(b).replace(".", ""));
|
|
59
|
+
return intA / intB * Math.pow(10, lenB - lenA);
|
|
60
|
+
}
|
|
61
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
62
|
+
0 && (module.exports = {
|
|
63
|
+
add,
|
|
64
|
+
divide,
|
|
65
|
+
multiply,
|
|
66
|
+
subtract
|
|
67
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/math.ts
|
|
2
|
+
function getDecimalLen(num) {
|
|
3
|
+
const str = String(num);
|
|
4
|
+
const index = str.indexOf(".");
|
|
5
|
+
return index === -1 ? 0 : str.length - index - 1;
|
|
6
|
+
}
|
|
7
|
+
function add(a, b) {
|
|
8
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b));
|
|
9
|
+
const base = Math.pow(10, maxLen);
|
|
10
|
+
return (Math.round(a * base) + Math.round(b * base)) / base;
|
|
11
|
+
}
|
|
12
|
+
function subtract(a, b) {
|
|
13
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b));
|
|
14
|
+
const base = Math.pow(10, maxLen);
|
|
15
|
+
return (Math.round(a * base) - Math.round(b * base)) / base;
|
|
16
|
+
}
|
|
17
|
+
function multiply(a, b) {
|
|
18
|
+
const lenA = getDecimalLen(a);
|
|
19
|
+
const lenB = getDecimalLen(b);
|
|
20
|
+
const intA = Number(String(a).replace(".", ""));
|
|
21
|
+
const intB = Number(String(b).replace(".", ""));
|
|
22
|
+
return intA * intB / Math.pow(10, lenA + lenB);
|
|
23
|
+
}
|
|
24
|
+
function divide(a, b) {
|
|
25
|
+
if (b === 0) throw new Error("\u9664\u6570\u4E0D\u80FD\u4E3A 0");
|
|
26
|
+
const lenA = getDecimalLen(a);
|
|
27
|
+
const lenB = getDecimalLen(b);
|
|
28
|
+
const intA = Number(String(a).replace(".", ""));
|
|
29
|
+
const intB = Number(String(b).replace(".", ""));
|
|
30
|
+
return intA / intB * Math.pow(10, lenB - lenA);
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
add,
|
|
34
|
+
divide,
|
|
35
|
+
multiply,
|
|
36
|
+
subtract
|
|
37
|
+
};
|
package/package.json
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { add, subtract, multiply, divide } from './math'
|
package/src/math.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function getDecimalLen(num: number): number {
|
|
2
|
+
const str = String(num)
|
|
3
|
+
const index = str.indexOf('.')
|
|
4
|
+
return index === -1 ? 0 : str.length - index - 1
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** 精确加法 */
|
|
8
|
+
export function add(a: number, b: number): number {
|
|
9
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b))
|
|
10
|
+
const base = Math.pow(10, maxLen)
|
|
11
|
+
return (Math.round(a * base) + Math.round(b * base)) / base
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 精确减法 */
|
|
15
|
+
export function subtract(a: number, b: number): number {
|
|
16
|
+
const maxLen = Math.max(getDecimalLen(a), getDecimalLen(b))
|
|
17
|
+
const base = Math.pow(10, maxLen)
|
|
18
|
+
return (Math.round(a * base) - Math.round(b * base)) / base
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 精确乘法 */
|
|
22
|
+
export function multiply(a: number, b: number): number {
|
|
23
|
+
const lenA = getDecimalLen(a)
|
|
24
|
+
const lenB = getDecimalLen(b)
|
|
25
|
+
const intA = Number(String(a).replace('.', ''))
|
|
26
|
+
const intB = Number(String(b).replace('.', ''))
|
|
27
|
+
return (intA * intB) / Math.pow(10, lenA + lenB)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 精确除法 */
|
|
31
|
+
export function divide(a: number, b: number): number {
|
|
32
|
+
if (b === 0) throw new Error('除数不能为 0')
|
|
33
|
+
const lenA = getDecimalLen(a)
|
|
34
|
+
const lenB = getDecimalLen(b)
|
|
35
|
+
const intA = Number(String(a).replace('.', ''))
|
|
36
|
+
const intB = Number(String(b).replace('.', ''))
|
|
37
|
+
return (intA / intB) * Math.pow(10, lenB - lenA)
|
|
38
|
+
}
|
package/tsconfig.json
ADDED