amountutil 1.0.0 → 2.0.2
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/AmountUtil.js +41 -11
- package/README.md +22 -7
- package/package.json +1 -1
package/AmountUtil.js
CHANGED
|
@@ -2,7 +2,7 @@ class AmountUtil {
|
|
|
2
2
|
#amount = ''
|
|
3
3
|
|
|
4
4
|
constructor(amount) {
|
|
5
|
-
this.#amount = amount
|
|
5
|
+
this.#amount = amount + ''
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
#compare = (num1, num2) => {
|
|
@@ -43,6 +43,16 @@ class AmountUtil {
|
|
|
43
43
|
return 1
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
num1 = num1.replace(/^0+/, '')
|
|
47
|
+
num2 = num2.replace(/^0+/, '')
|
|
48
|
+
|
|
49
|
+
if (num1.includes('.')) {
|
|
50
|
+
num1 = num1.replace(/0+$/, '')
|
|
51
|
+
}
|
|
52
|
+
if (num2.includes('.')) {
|
|
53
|
+
num2 = num2.replace(/0+$/, '')
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
if (!num1.includes('.')) {
|
|
47
57
|
num1 += '.0'
|
|
48
58
|
}
|
|
@@ -436,13 +446,13 @@ class AmountUtil {
|
|
|
436
446
|
|
|
437
447
|
if (this.#compare(curDividend, divisor) === 1 || this.#compare(curDividend, divisor) === 0) {
|
|
438
448
|
let n = '1'
|
|
439
|
-
while (this.#compare(this.#multiply(divisor, n
|
|
449
|
+
while (this.#compare(this.#multiply(divisor, n), curDividend) === -1) {
|
|
440
450
|
n = this.#plus(n, '1')
|
|
441
451
|
}
|
|
442
452
|
|
|
443
|
-
if (this.#compare(curDividend, divisor) === 0) {
|
|
453
|
+
if (this.#compare(curDividend, this.#multiply(divisor, n)) === 0) {
|
|
444
454
|
result += n
|
|
445
|
-
curDividend = '
|
|
455
|
+
curDividend = ''
|
|
446
456
|
} else {
|
|
447
457
|
result += this.#minus(n, '1')
|
|
448
458
|
curDividend = this.#minus(curDividend, this.#multiply(divisor, this.#minus(n, '1')))
|
|
@@ -464,8 +474,20 @@ class AmountUtil {
|
|
|
464
474
|
result = this.#round(result, precision)
|
|
465
475
|
} else {
|
|
466
476
|
let decimalLength = result.match(/\.(\d+)/)[1].length
|
|
467
|
-
|
|
468
|
-
|
|
477
|
+
|
|
478
|
+
if (decimalLength !== null || decimalLength !== undefined || decimalLength !== 0) {
|
|
479
|
+
result = result.replace(/0+$/, '')
|
|
480
|
+
|
|
481
|
+
if (result[result.length - 1] === '.') result = result.slice(0, -1)
|
|
482
|
+
|
|
483
|
+
if (result.match(/\.(\d+)/)) decimalLength = result.match(/\.(\d+)/)[1].length
|
|
484
|
+
else decimalLength = 0
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (decimalLength !== null || decimalLength !== undefined || decimalLength !== 0) {
|
|
488
|
+
if (this.#compare(decimalLength, precision) === 1) {
|
|
489
|
+
result = this.#round(result, precision)
|
|
490
|
+
}
|
|
469
491
|
}
|
|
470
492
|
}
|
|
471
493
|
|
|
@@ -624,7 +646,7 @@ class AmountUtil {
|
|
|
624
646
|
}
|
|
625
647
|
|
|
626
648
|
compare = (num) => {
|
|
627
|
-
return this.#compare(this.#amount, num)
|
|
649
|
+
return this.#compare(this.#amount, num.toString())
|
|
628
650
|
}
|
|
629
651
|
|
|
630
652
|
toFixed = (precision) => {
|
|
@@ -632,22 +654,22 @@ class AmountUtil {
|
|
|
632
654
|
}
|
|
633
655
|
|
|
634
656
|
add = (num) => {
|
|
635
|
-
this.#amount = this.#plus(this.#amount, num)
|
|
657
|
+
this.#amount = this.#plus(this.#amount, num.toString())
|
|
636
658
|
return this
|
|
637
659
|
}
|
|
638
660
|
|
|
639
661
|
subtract = (num) => {
|
|
640
|
-
this.#amount = this.#minus(this.#amount, num)
|
|
662
|
+
this.#amount = this.#minus(this.#amount, num.toString())
|
|
641
663
|
return this
|
|
642
664
|
}
|
|
643
665
|
|
|
644
666
|
multiply = (num) => {
|
|
645
|
-
this.#amount = this.#multiply(this.#amount, num)
|
|
667
|
+
this.#amount = this.#multiply(this.#amount, num.toString())
|
|
646
668
|
return this
|
|
647
669
|
}
|
|
648
670
|
|
|
649
671
|
divide = (num, precision = 19, calculatedRound = false) => {
|
|
650
|
-
this.#amount = this.#divide(this.#amount, num, precision, calculatedRound)
|
|
672
|
+
this.#amount = this.#divide(this.#amount, num.toString(), precision, calculatedRound)
|
|
651
673
|
return this
|
|
652
674
|
}
|
|
653
675
|
|
|
@@ -659,6 +681,14 @@ class AmountUtil {
|
|
|
659
681
|
return this.#thousandSeparator(this.#amount)
|
|
660
682
|
}
|
|
661
683
|
|
|
684
|
+
static of(amount) {
|
|
685
|
+
return new AmountUtil(amount)
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
static create(amount) {
|
|
689
|
+
return new AmountUtil(amount)
|
|
690
|
+
}
|
|
691
|
+
|
|
662
692
|
toString = () => {
|
|
663
693
|
return this.#amount
|
|
664
694
|
}
|
package/README.md
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
# AmountUtil
|
|
1
|
+
# AmountUtil 金额工具
|
|
2
2
|
|
|
3
|
+
这是一个可以帮助你管理金额的工具,且我们引入了一个非常强大的中文金额翻译功能。
|
|
3
4
|
The util that can help you to manage about amount,we have introduced a very powerful Chinese amount translation feature.
|
|
4
5
|
|
|
5
|
-
## Installation
|
|
6
|
+
## Installation 安装
|
|
6
7
|
|
|
7
8
|
```bash
|
|
8
9
|
npm install amountutil
|
|
9
10
|
```
|
|
10
11
|
|
|
11
|
-
## Usage
|
|
12
|
+
## Usage 使用
|
|
12
13
|
|
|
13
14
|
```javascript
|
|
14
15
|
const AmountUtil = require('amountutil');
|
|
15
16
|
|
|
16
17
|
// 转换为中文金额
|
|
17
|
-
let amount =
|
|
18
|
+
let amount = AmountUtil.of(123456.78); // of方法、create方法可传入字符串,返回的是AmountUtil实例
|
|
18
19
|
const chineseAmount = amount.translateAmount();
|
|
19
20
|
console.log(chineseAmount); // 输出:"壹拾贰万叁仟肆佰伍拾陆元柒角捌分"
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
-
let amount1 =
|
|
23
|
-
let sum = amount1.add(98765.43);
|
|
22
|
+
// 大数加减乘除,加减乘除方法返回的是实例本身,即可以链式调用加减乘除
|
|
23
|
+
let amount1 = AmountUtil.of(123456.78);
|
|
24
|
+
let sum = amount1.add(AmountUtil.of(98765.43)); // 此处可以直接写数字:amount1.add(98765.43)
|
|
24
25
|
console.log(sum.toString()); // 输出:222222.21
|
|
25
26
|
|
|
26
27
|
let diff = amount1.subtract(98765.43);
|
|
@@ -36,4 +37,18 @@ console.log(quotient.toString()); // 输出:123456.78
|
|
|
36
37
|
console.log(amount1.compare(98765.43)); // 输出:1
|
|
37
38
|
console.log(amount1.toFixed(1)); // 输出:123456.8
|
|
38
39
|
console.log(amount1.toLocaleString()); // 输出:123,456.78
|
|
40
|
+
|
|
41
|
+
// 注:除法默认保留19位小数,可通过第二个参数指定保留小数位数,第三个参数指定是否四舍五入
|
|
42
|
+
let amount2 = AmountUtil.create(100);
|
|
43
|
+
let quotient2 = amount2.divide(10, precision = 2, true);
|
|
44
|
+
console.log(quotient2.toString()); // 输出:10.00
|
|
45
|
+
|
|
46
|
+
let amount3 = AmountUtil.create(100);
|
|
47
|
+
let quotient3 = amount3.divide(10, precision = 2, false);
|
|
48
|
+
console.log(quotient3.toString()); // 输出:10
|
|
49
|
+
|
|
50
|
+
// precision用于无限循环小数的情况,对于可以整除的情况,可以考虑加入第三个参数来确定是否保留小数位数,如下
|
|
51
|
+
let amount4 = AmountUtil.create(120.323);
|
|
52
|
+
let quotient4 = amount4.divide(3, precision = 21); // 不传precision参数,默认保留19位小数,第三个参数默认为false,此时传递true时,不会有影响,因为precision参数已经影响了小数位数
|
|
53
|
+
console.log(quotient4.toString()); // 输出:40.107666666666666666667
|
|
39
54
|
```
|
package/package.json
CHANGED