@fnmain/number 1.0.0 → 1.1.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 CHANGED
@@ -1,10 +1,12 @@
1
1
  type Options = {
2
2
  fixed?: number;
3
+ trimEndZeros?: boolean;
3
4
  space?: string;
4
5
  };
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;
6
+ export declare function trimTrailingZeros(numberString: string): string;
7
+ export declare function toWan(value: number, { fixed, trimEndZeros, space }?: Options): string;
8
+ export declare function toYi(value: number, { fixed, trimEndZeros, space }?: Options): string;
9
+ export declare function toWanYi(value: number, { fixed, trimEndZeros, space }?: Options): string;
10
+ export declare function toAuto(value: number, { fixed, trimEndZeros, space }?: Options): string;
11
+ export declare function toPercent(value: number, { fixed, trimEndZeros, space }?: Options): string;
10
12
  export {};
package/dist/index.js CHANGED
@@ -1,33 +1,53 @@
1
1
  "use strict";
2
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 + "万";
3
+ exports.toPercent = exports.toAuto = exports.toWanYi = exports.toYi = exports.toWan = exports.trimTrailingZeros = void 0;
4
+ function trimTrailingZeros(numberString) {
5
+ // Check if the string contains a decimal point
6
+ if (numberString.includes(".")) {
7
+ // Remove trailing zeros after a decimal point and the decimal point if necessary
8
+ return numberString.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1");
9
+ }
10
+ // Return the original string if there is no decimal point
11
+ return numberString;
12
+ }
13
+ exports.trimTrailingZeros = trimTrailingZeros;
14
+ function toWan(value, { fixed = 2, trimEndZeros = false, space = "" } = {}) {
15
+ let ret = (value / 10000).toFixed(fixed);
16
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
17
+ return ret + space + "万";
6
18
  }
7
19
  exports.toWan = toWan;
8
- function toYi(value, { fixed = 0, space = "" } = {}) {
9
- return (value / 100000000).toFixed(fixed) + space + "亿";
20
+ function toYi(value, { fixed = 2, trimEndZeros = false, space = "" } = {}) {
21
+ let ret = (value / 100000000).toFixed(fixed);
22
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
23
+ return ret + space + "亿";
10
24
  }
11
25
  exports.toYi = toYi;
12
- function toWanYi(value, { fixed = 0, space = "" } = {}) {
13
- return (value / 1000000000000).toFixed(fixed) + space + "万亿";
26
+ function toWanYi(value, { fixed = 2, trimEndZeros = false, space = "" } = {}) {
27
+ let ret = (value / 1000000000000).toFixed(fixed);
28
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
29
+ return ret + space + "万亿";
14
30
  }
15
31
  exports.toWanYi = toWanYi;
16
- function toAuto(value, { fixed = 0, space = "" } = {}) {
32
+ function toAuto(value, { fixed = 2, trimEndZeros = false, space = "" } = {}) {
17
33
  const abs = Math.abs(value);
18
34
  if (abs < 10000) {
19
- return value.toString();
35
+ let ret = value.toString();
36
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
37
+ return ret;
20
38
  }
21
39
  if (abs < 100000000) {
22
- return toWan(value, { fixed, space });
40
+ return toWan(value, { fixed, trimEndZeros, space });
23
41
  }
24
42
  if (abs < 1000000000000) {
25
- return toYi(value, { fixed, space });
43
+ return toYi(value, { fixed, trimEndZeros, space });
26
44
  }
27
- return toWanYi(value, { fixed, space });
45
+ return toWanYi(value, { fixed, trimEndZeros, space });
28
46
  }
29
47
  exports.toAuto = toAuto;
30
- function toPercent(value, { fixed = 0, space = "" } = {}) {
31
- return (value * 100).toFixed(fixed) + space + "%";
48
+ function toPercent(value, { fixed = 2, trimEndZeros = false, space = "" } = {}) {
49
+ let ret = (value * 100).toFixed(fixed);
50
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
51
+ return ret + space + "%";
32
52
  }
33
53
  exports.toPercent = toPercent;
package/dist/test.js CHANGED
@@ -8,5 +8,7 @@ console.log((0, index_1.toAuto)(1234, { fixed: 1 }));
8
8
  console.log((0, index_1.toAuto)(12345, { fixed: 1 }));
9
9
  console.log((0, index_1.toAuto)(123456789, { fixed: 2, space: " " }));
10
10
  console.log((0, index_1.toAuto)(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " }));
11
+ console.log((0, index_1.toAuto)(1.2, { fixed: 4, trimEndZeros: true }));
11
12
  console.log((0, index_1.toPercent)(0.1234, { fixed: 1, space: " " }));
12
- console.log((0, index_1.toPercent)(1.234));
13
+ console.log((0, index_1.toPercent)(1.23456, { trimEndZeros: true }));
14
+ console.log((0, index_1.toPercent)(1.9, { fixed: 4, trimEndZeros: true }));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@fnmain/number",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "number utils",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "node dist/test.js"
9
+ "test": "tsc && node dist/test.js"
10
10
  },
11
11
  "author": "DC",
12
12
  "license": "MIT",
package/src/index.ts CHANGED
@@ -1,34 +1,70 @@
1
1
  type Options = {
2
2
  fixed?: number;
3
+ trimEndZeros?: boolean;
3
4
  space?: string;
4
5
  };
5
6
 
6
- export function toWan(value: number, { fixed = 0, space = "" }: Options = {}): string {
7
- return (value / 10000).toFixed(fixed) + space + "万";
7
+ export function trimTrailingZeros(numberString: string): string {
8
+ // Check if the string contains a decimal point
9
+ if (numberString.includes(".")) {
10
+ // Remove trailing zeros after a decimal point and the decimal point if necessary
11
+ return numberString.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1");
12
+ }
13
+ // Return the original string if there is no decimal point
14
+ return numberString;
15
+ }
16
+
17
+ export function toWan(
18
+ value: number,
19
+ { fixed = 2, trimEndZeros = false, space = "" }: Options = {}
20
+ ): string {
21
+ let ret = (value / 10000).toFixed(fixed);
22
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
23
+ return ret + space + "万";
8
24
  }
9
25
 
10
- export function toYi(value: number, { fixed = 0, space = "" }: Options = {}): string {
11
- return (value / 100000000).toFixed(fixed) + space + "亿";
26
+ export function toYi(
27
+ value: number,
28
+ { fixed = 2, trimEndZeros = false, space = "" }: Options = {}
29
+ ): string {
30
+ let ret = (value / 100000000).toFixed(fixed);
31
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
32
+ return ret + space + "亿";
12
33
  }
13
34
 
14
- export function toWanYi(value: number, { fixed = 0, space = "" }: Options = {}): string {
15
- return (value / 1000000000000).toFixed(fixed) + space + "万亿";
35
+ export function toWanYi(
36
+ value: number,
37
+ { fixed = 2, trimEndZeros = false, space = "" }: Options = {}
38
+ ): string {
39
+ let ret = (value / 1000000000000).toFixed(fixed);
40
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
41
+ return ret + space + "万亿";
16
42
  }
17
43
 
18
- export function toAuto(value: number, { fixed = 0, space = "" }: Options = {}): string {
44
+ export function toAuto(
45
+ value: number,
46
+ { fixed = 2, trimEndZeros = false, space = "" }: Options = {}
47
+ ): string {
19
48
  const abs = Math.abs(value);
20
49
  if (abs < 10000) {
21
- return value.toString();
50
+ let ret = value.toString();
51
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
52
+ return ret;
22
53
  }
23
54
  if (abs < 100000000) {
24
- return toWan(value, { fixed, space });
55
+ return toWan(value, { fixed, trimEndZeros, space });
25
56
  }
26
57
  if (abs < 1000000000000) {
27
- return toYi(value, { fixed, space });
58
+ return toYi(value, { fixed, trimEndZeros, space });
28
59
  }
29
- return toWanYi(value, { fixed, space });
60
+ return toWanYi(value, { fixed, trimEndZeros, space });
30
61
  }
31
62
 
32
- export function toPercent(value: number, { fixed = 0, space = "" }: Options = {}) {
33
- return (value * 100).toFixed(fixed) + space + "%";
63
+ export function toPercent(
64
+ value: number,
65
+ { fixed = 2, trimEndZeros = false, space = "" }: Options = {}
66
+ ) {
67
+ let ret = (value * 100).toFixed(fixed);
68
+ ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
69
+ return ret + space + "%";
34
70
  }
package/src/test.ts CHANGED
@@ -8,6 +8,8 @@ console.log(toAuto(1234, { fixed: 1 }));
8
8
  console.log(toAuto(12345, { fixed: 1 }));
9
9
  console.log(toAuto(123456789, { fixed: 2, space: " " }));
10
10
  console.log(toAuto(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " }));
11
+ console.log(toAuto(1.2, { fixed: 4, trimEndZeros: true }));
11
12
 
12
13
  console.log(toPercent(0.1234, { fixed: 1, space: " " }));
13
- console.log(toPercent(1.234));
14
+ console.log(toPercent(1.23456, { trimEndZeros: true }));
15
+ console.log(toPercent(1.9, { fixed: 4, trimEndZeros: true }));