@fnmain/number 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/dist/index.d.ts +10 -0
- package/dist/index.js +33 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +12 -0
- package/package.json +17 -0
- package/src/index.ts +34 -0
- package/src/test.ts +13 -0
- package/tsconfig.json +14 -0
package/dist/index.d.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
type Options = {
|
2
|
+
fixed?: number;
|
3
|
+
space?: string;
|
4
|
+
};
|
5
|
+
export declare function toWan(value: number, { fixed, space }?: Options): string;
|
6
|
+
export declare function toYi(value: number, { fixed, space }?: Options): string;
|
7
|
+
export declare function toWanYi(value: number, { fixed, space }?: Options): string;
|
8
|
+
export declare function toAuto(value: number, { fixed, space }?: Options): string;
|
9
|
+
export declare function toPercent(value: number, { fixed, space }?: Options): string;
|
10
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.toPercent = exports.toAuto = exports.toWanYi = exports.toYi = exports.toWan = void 0;
|
4
|
+
function toWan(value, { fixed = 0, space = "" } = {}) {
|
5
|
+
return (value / 10000).toFixed(fixed) + space + "万";
|
6
|
+
}
|
7
|
+
exports.toWan = toWan;
|
8
|
+
function toYi(value, { fixed = 0, space = "" } = {}) {
|
9
|
+
return (value / 100000000).toFixed(fixed) + space + "亿";
|
10
|
+
}
|
11
|
+
exports.toYi = toYi;
|
12
|
+
function toWanYi(value, { fixed = 0, space = "" } = {}) {
|
13
|
+
return (value / 1000000000000).toFixed(fixed) + space + "万亿";
|
14
|
+
}
|
15
|
+
exports.toWanYi = toWanYi;
|
16
|
+
function toAuto(value, { fixed = 0, space = "" } = {}) {
|
17
|
+
const abs = Math.abs(value);
|
18
|
+
if (abs < 10000) {
|
19
|
+
return value.toString();
|
20
|
+
}
|
21
|
+
if (abs < 100000000) {
|
22
|
+
return toWan(value, { fixed, space });
|
23
|
+
}
|
24
|
+
if (abs < 1000000000000) {
|
25
|
+
return toYi(value, { fixed, space });
|
26
|
+
}
|
27
|
+
return toWanYi(value, { fixed, space });
|
28
|
+
}
|
29
|
+
exports.toAuto = toAuto;
|
30
|
+
function toPercent(value, { fixed = 0, space = "" } = {}) {
|
31
|
+
return (value * 100).toFixed(fixed) + space + "%";
|
32
|
+
}
|
33
|
+
exports.toPercent = toPercent;
|
package/dist/test.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const index_1 = require("./index");
|
4
|
+
console.log((0, index_1.toAuto)(1234));
|
5
|
+
console.log((0, index_1.toAuto)(12345));
|
6
|
+
console.log((0, index_1.toAuto)(123456789));
|
7
|
+
console.log((0, index_1.toAuto)(1234, { fixed: 1 }));
|
8
|
+
console.log((0, index_1.toAuto)(12345, { fixed: 1 }));
|
9
|
+
console.log((0, index_1.toAuto)(123456789, { fixed: 2, space: " " }));
|
10
|
+
console.log((0, index_1.toAuto)(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " }));
|
11
|
+
console.log((0, index_1.toPercent)(0.1234, { fixed: 1, space: " " }));
|
12
|
+
console.log((0, index_1.toPercent)(1.234));
|
package/package.json
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"name": "@fnmain/number",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "number utils",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"scripts": {
|
8
|
+
"build": "tsc",
|
9
|
+
"test": "node dist/test.js"
|
10
|
+
},
|
11
|
+
"author": "DC",
|
12
|
+
"license": "MIT",
|
13
|
+
"devDependencies": {
|
14
|
+
"@types/node": "^20.8.3",
|
15
|
+
"typescript": "^5.2.2"
|
16
|
+
}
|
17
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
type Options = {
|
2
|
+
fixed?: number;
|
3
|
+
space?: string;
|
4
|
+
};
|
5
|
+
|
6
|
+
export function toWan(value: number, { fixed = 0, space = "" }: Options = {}): string {
|
7
|
+
return (value / 10000).toFixed(fixed) + space + "万";
|
8
|
+
}
|
9
|
+
|
10
|
+
export function toYi(value: number, { fixed = 0, space = "" }: Options = {}): string {
|
11
|
+
return (value / 100000000).toFixed(fixed) + space + "亿";
|
12
|
+
}
|
13
|
+
|
14
|
+
export function toWanYi(value: number, { fixed = 0, space = "" }: Options = {}): string {
|
15
|
+
return (value / 1000000000000).toFixed(fixed) + space + "万亿";
|
16
|
+
}
|
17
|
+
|
18
|
+
export function toAuto(value: number, { fixed = 0, space = "" }: Options = {}): string {
|
19
|
+
const abs = Math.abs(value);
|
20
|
+
if (abs < 10000) {
|
21
|
+
return value.toString();
|
22
|
+
}
|
23
|
+
if (abs < 100000000) {
|
24
|
+
return toWan(value, { fixed, space });
|
25
|
+
}
|
26
|
+
if (abs < 1000000000000) {
|
27
|
+
return toYi(value, { fixed, space });
|
28
|
+
}
|
29
|
+
return toWanYi(value, { fixed, space });
|
30
|
+
}
|
31
|
+
|
32
|
+
export function toPercent(value: number, { fixed = 0, space = "" }: Options = {}) {
|
33
|
+
return (value * 100).toFixed(fixed) + space + "%";
|
34
|
+
}
|
package/src/test.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import { toAuto, toPercent } from "./index";
|
2
|
+
|
3
|
+
console.log(toAuto(1234));
|
4
|
+
console.log(toAuto(12345));
|
5
|
+
console.log(toAuto(123456789));
|
6
|
+
|
7
|
+
console.log(toAuto(1234, { fixed: 1 }));
|
8
|
+
console.log(toAuto(12345, { fixed: 1 }));
|
9
|
+
console.log(toAuto(123456789, { fixed: 2, space: " " }));
|
10
|
+
console.log(toAuto(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " }));
|
11
|
+
|
12
|
+
console.log(toPercent(0.1234, { fixed: 1, space: " " }));
|
13
|
+
console.log(toPercent(1.234));
|
package/tsconfig.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "es2016",
|
4
|
+
"module": "commonjs",
|
5
|
+
"outDir": "./dist",
|
6
|
+
"strict": true,
|
7
|
+
"esModuleInterop": true,
|
8
|
+
"skipLibCheck": true,
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
10
|
+
"declaration": true
|
11
|
+
},
|
12
|
+
"include": ["src/**/*.ts"],
|
13
|
+
"exclude": ["node_modules"]
|
14
|
+
}
|