amountutil 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.
Files changed (3) hide show
  1. package/AmountUtil.js +669 -0
  2. package/README.md +39 -0
  3. package/package.json +18 -0
package/AmountUtil.js ADDED
@@ -0,0 +1,669 @@
1
+ class AmountUtil {
2
+ #amount = ''
3
+
4
+ constructor(amount) {
5
+ this.#amount = amount
6
+ }
7
+
8
+ #compare = (num1, num2) => {
9
+ num1 = num1 + ''
10
+ num2 = num2 + ''
11
+
12
+ if (Number(num1) === 0 && Number(num2) === 0) return 0
13
+
14
+ if (Number(num1) === 0) {
15
+ if (Number(num2) === 0) {
16
+ return 0
17
+ } else {
18
+ if (num2[0] === '-') {
19
+ return 1
20
+ } else {
21
+ return -1
22
+ }
23
+ }
24
+ } else if (Number(num2) === 0) {
25
+ if (Number(num1) === 0) {
26
+ return 0
27
+ } else {
28
+ if (num1[0] === '-') {
29
+ return -1
30
+ } else {
31
+ return 1
32
+ }
33
+ }
34
+ }
35
+
36
+ if (num1[0] === '-') {
37
+ if (num2[0] === '-') {
38
+ return this.#compare(num1.slice(1), num2.slice(1)) * -1
39
+ } else {
40
+ return -1
41
+ }
42
+ } else if (num2[0] === '-') {
43
+ return 1
44
+ }
45
+
46
+ if (!num1.includes('.')) {
47
+ num1 += '.0'
48
+ }
49
+ if (!num2.includes('.')) {
50
+ num2 += '.0'
51
+ }
52
+
53
+ const integer1 = num1.split('.')[0]
54
+ const integer2 = num2.split('.')[0]
55
+ let decimal1 = num1.split('.')[1]
56
+ let decimal2 = num2.split('.')[1]
57
+ let integerCompareResult = integer1.length > integer2.length ? 1 : integer1.length < integer2.length ? -1 : 0
58
+
59
+ if (integerCompareResult !== 0) {
60
+ return integerCompareResult
61
+ } else {
62
+ for (let i = 0; i < integer1.length; i++) {
63
+ if (integer1[i] > integer2[i]) {
64
+ integerCompareResult = 1
65
+ break
66
+ } else if (integer1[i] < integer2[i]) {
67
+ integerCompareResult = -1
68
+ break
69
+ }
70
+ }
71
+
72
+ if (integerCompareResult !== 0) {
73
+ return integerCompareResult
74
+ }
75
+
76
+ integerCompareResult = 0
77
+ }
78
+
79
+ if (integerCompareResult === 0) {
80
+ decimal1 = decimal1.replace(/0+$/, '')
81
+ decimal2 = decimal2.replace(/0+$/, '')
82
+
83
+ if (decimal1.trim().length === 0) {
84
+ decimal1 = '0'
85
+ }
86
+ if (decimal2.trim().length === 0) {
87
+ decimal2 = '0'
88
+ }
89
+
90
+ if (decimal1.length > decimal2.length) {
91
+ decimal2 += '0'.repeat(decimal1.length - decimal2.length)
92
+ } else if (decimal1.length < decimal2.length) {
93
+ decimal1 += '0'.repeat(decimal2.length - decimal1.length)
94
+ }
95
+
96
+ for (let i = 0; i < decimal1.length; i++) {
97
+ if (decimal1[i] > decimal2[i]) {
98
+ return 1
99
+ } else if (decimal1[i] < decimal2[i]) {
100
+ return -1
101
+ }
102
+ }
103
+
104
+ return 0
105
+ }
106
+ }
107
+
108
+ #round = (num, precision = 0) => {
109
+ num = num + ''
110
+ if (num.trim().length === 0 || precision < 0) {
111
+ return '0'
112
+ }
113
+
114
+ if (!num.includes('.')) {
115
+ num += '.0'
116
+ }
117
+
118
+ if (num[0] === '-') {
119
+ return '-' + this.#round(num.slice(1), precision)
120
+ }
121
+
122
+ let integerPart = num.split('.')[0]
123
+ let decimalPart = num.split('.')[1]
124
+
125
+ if (precision > decimalPart.length) {
126
+ decimalPart += '0'.repeat(precision - decimalPart.length)
127
+ return integerPart + '.' + decimalPart
128
+ } else if (precision === decimalPart.length) {
129
+ return integerPart + '.' + decimalPart
130
+ }
131
+
132
+ if (precision === 0) {
133
+ return this.#compare(decimalPart[0], '5') === 1 || this.#compare(decimalPart[0], '5') === 0 ? this.#plus(integerPart, '1').split('.')[0] : integerPart
134
+ } else {
135
+ if (this.#compare(decimalPart[precision], '5') === 1 || this.#compare(decimalPart[precision], '5') === 0) {
136
+ let m = ''
137
+ if (integerPart[0] === '-') {
138
+ m = '-'
139
+ integerPart = integerPart.slice(1)
140
+ }
141
+ let newNum = integerPart + '.' + decimalPart.slice(0, precision)
142
+ let addNum = '0.' + '0'.repeat(precision - 1) + '1'
143
+ let result = m + this.#plus(newNum, addNum)
144
+ if (result.split('.')[1]?.length < precision) {
145
+ result += '0'.repeat(precision - result.split('.')[1].length)
146
+ }
147
+
148
+ if (!result.includes('.')) {
149
+ result += '.' + '0'.repeat(precision)
150
+ }
151
+
152
+ return result
153
+ } else {
154
+ return integerPart + '.' + decimalPart.slice(0, precision)
155
+ }
156
+ }
157
+ }
158
+
159
+ #plus = (addend1, addend2) => {
160
+ addend1 = addend1 + ''
161
+ addend2 = addend2 + ''
162
+ if (addend1.trim().length === 0 || addend2.trim().length === 0) {
163
+ return '0'
164
+ }
165
+
166
+ if (this.#compare(addend1, '0') === 0 && this.#compare(addend2, '0') === 0) return '0'
167
+
168
+ if (addend2[0] === '-') {
169
+ if (addend1[0] === '-') {
170
+ return '-' + this.#plus(addend1.slice(1), addend2.slice(1))
171
+ } else {
172
+ let r = this.#minus(addend2.slice(1), addend1)
173
+
174
+ if (r[0] === '-') {
175
+ return r.slice(1)
176
+ } else {
177
+ return '-' + r
178
+ }
179
+ }
180
+ } else if (addend1[0] === '-') {
181
+ return this.#minus(addend2, addend1.slice(1))
182
+ }
183
+
184
+ if (!addend1.includes('.')) {
185
+ addend1 += '.0'
186
+ }
187
+ if (!addend2.includes('.')) {
188
+ addend2 += '.0'
189
+ }
190
+
191
+ let integer1 = addend1.split('.')[0]
192
+ let integer2 = addend2.split('.')[0]
193
+ let decimal1 = addend1.split('.')[1]
194
+ let decimal2 = addend2.split('.')[1]
195
+
196
+ if (decimal1.length > decimal2.length) {
197
+ decimal2 += '0'.repeat(decimal1.length - decimal2.length)
198
+ } else if (decimal1.length < decimal2.length) {
199
+ decimal1 += '0'.repeat(decimal2.length - decimal1.length)
200
+ }
201
+
202
+ if (integer1.length > integer2.length) {
203
+ integer2 = '0'.repeat(integer1.length - integer2.length) + integer2
204
+ } else if (integer1.length < integer2.length) {
205
+ integer1 = '0'.repeat(integer2.length - integer1.length) + integer1
206
+ }
207
+
208
+ let addDecimal1 = integer1 + '.' + decimal1
209
+ let addDecimal2 = integer2 + '.' + decimal2
210
+
211
+ let count = addDecimal1.length
212
+ let carry = 0;
213
+ let currentSum = 0;
214
+ let resultArray = [];
215
+
216
+ for (let i = count - 1; i >= 0; i--) {
217
+ if (addDecimal1[i] === '.') {
218
+ resultArray = [ '.', ...resultArray ]
219
+ continue
220
+ }
221
+ currentSum = Number(addDecimal1[i]) + Number(addDecimal2[i]) + carry
222
+ carry = Math.floor(currentSum / 10)
223
+ resultArray = [ (currentSum % 10).toString(), ...resultArray ]
224
+ }
225
+
226
+ if (carry !== 0) resultArray = [ carry.toString(), ...resultArray ]
227
+
228
+ let result = resultArray.join('').replace(/0+$/, '')
229
+
230
+ return result[result.length - 1] === '.' ? result.slice(0, -1) : result
231
+ }
232
+
233
+ #minus = (minuend, subtrahend) => {
234
+ minuend = minuend + ''
235
+ subtrahend = subtrahend + ''
236
+ if (minuend.trim().length === 0 || subtrahend.trim().length === 0) {
237
+ return '0'
238
+ }
239
+
240
+ if (this.#compare(minuend, '0') === 0 && this.#compare(subtrahend, '0') === 0) return '0'
241
+
242
+ if (subtrahend[0] === '-') {
243
+ if (minuend[0] === '-') {
244
+ return this.#minus(subtrahend.slice(1), minuend.slice(1))
245
+ } else {
246
+ return this.#plus(minuend, subtrahend.slice(1))
247
+ }
248
+ } else if (minuend[0] === '-') {
249
+ return '-' + this.#plus(minuend.slice(1), subtrahend)
250
+ }
251
+
252
+ if (this.#compare(minuend, subtrahend) === 0) {
253
+ return '0'
254
+ } else if (this.#compare(minuend, subtrahend) === -1) {
255
+ return '-' + this.#minus(subtrahend, minuend)
256
+ }
257
+
258
+ if (!minuend.includes('.')) {
259
+ minuend += '.0'
260
+ }
261
+ if (!subtrahend.includes('.')) {
262
+ subtrahend += '.0'
263
+ }
264
+
265
+ let integerMinuend = minuend.split('.')[0]
266
+ let integerSubtrahend = subtrahend.split('.')[0]
267
+ let decimalMinuend = minuend.split('.')[1]
268
+ let decimalSubtrahend = subtrahend.split('.')[1]
269
+
270
+ if (integerMinuend.length > integerSubtrahend.length) {
271
+ integerSubtrahend = '0'.repeat(integerMinuend.length - integerSubtrahend.length) + integerSubtrahend
272
+ } else if (integerMinuend.length < integerSubtrahend.length) {
273
+ integerMinuend = '0'.repeat(integerSubtrahend.length - integerMinuend.length) + integerMinuend
274
+ }
275
+
276
+ if (decimalMinuend.length > decimalSubtrahend.length) {
277
+ decimalSubtrahend += '0'.repeat(decimalMinuend.length - decimalSubtrahend.length)
278
+ } else if (decimalMinuend.length < decimalSubtrahend.length) {
279
+ decimalMinuend += '0'.repeat(decimalSubtrahend.length - decimalMinuend.length)
280
+ }
281
+
282
+ let num1 = integerMinuend + '.' + decimalMinuend
283
+ let num2 = integerSubtrahend + '.' + decimalSubtrahend
284
+
285
+ let borrow = 0
286
+ let alreadyBorrowed = false
287
+ let curBit;
288
+ let resultArray = [];
289
+
290
+ for (let i = num1.length - 1; i >= 0; i--) {
291
+ if (num1[i] === '.') {
292
+ resultArray = [ '.', ...resultArray ]
293
+ continue
294
+ }
295
+ curBit = Number(num1[i]) - Number(num2[i]) - borrow
296
+ if (curBit < 0) {
297
+ if (!alreadyBorrowed) {
298
+ alreadyBorrowed = true
299
+ }
300
+ curBit += 10
301
+ borrow = 1
302
+ } else {
303
+ alreadyBorrowed = false
304
+ borrow = 0
305
+ }
306
+ resultArray = [ curBit.toString(), ...resultArray ]
307
+ }
308
+
309
+ let result = resultArray.join('').replace(/0+$/, '').replace(/^0+/, '')
310
+
311
+ if (result[0] === '.') {
312
+ result = '0' + result
313
+ }
314
+
315
+ if (result[result.length - 1] === '.') {
316
+ result = result.slice(0, -1)
317
+ }
318
+
319
+ return result
320
+ }
321
+
322
+ #multiply = (num1, num2) => {
323
+ num1 = num1 + ''
324
+ num2 = num2 + ''
325
+
326
+ if (num1.trim().length === 0 || num2.trim().length === 0) {
327
+ return '0'
328
+ }
329
+
330
+ if (this.#compare(num1, '0') === 0 || this.#compare(num2, '0') === 0) return '0'
331
+
332
+ if (num1[0] === '-') {
333
+ if (num2[0] !== '-') {
334
+ return '-' + this.#multiply(num1.slice(1), num2)
335
+ } else {
336
+ return this.#multiply(num1.slice(1), num2.slice(1))
337
+ }
338
+ } else if (num2[0] === '-') {
339
+ return '-' + this.#multiply(num1, num2.slice(1))
340
+ }
341
+
342
+ let decimal1 = num1.match(/\.(\d+)/)
343
+ let decimal2 = num2.match(/\.(\d+)/)
344
+ let decimalLength = 0
345
+ if (decimal1 && decimal2) {
346
+ decimalLength = decimal1[1].length + decimal2[1].length
347
+ } else if (decimal1) {
348
+ decimalLength = decimal1[1].length
349
+ } else if (decimal2) {
350
+ decimalLength = decimal2[1].length
351
+ }
352
+
353
+ num1 = num1.replace('.', '')
354
+ num2 = num2.replace('.', '')
355
+
356
+ let result = '0'
357
+ for (let i = num2.length - 1; i >= 0; i--) {
358
+ let curBit = Number(num2[i])
359
+ let curResult = ''
360
+ let carry = 0
361
+ for (let j = num1.length - 1; j >= 0; j--) {
362
+ let product = curBit * Number(num1[j]) + carry
363
+ carry = Math.floor(product / 10)
364
+ curResult = (product % 10).toString() + curResult
365
+ }
366
+ if (carry !== 0) curResult = carry.toString() + curResult
367
+ curResult += '0'.repeat(num2.length - 1 - i)
368
+ result = this.#plus(result, curResult)
369
+ }
370
+
371
+ if (decimalLength > 0) {
372
+ result = result.slice(0, -decimalLength) + '.' + result.slice(-decimalLength)
373
+ }
374
+
375
+ result = result.replace(/^0+/, '')
376
+ if (result.length === 0 || result[0] === '.') {
377
+ result = '0' + result
378
+ }
379
+
380
+ return result
381
+ }
382
+
383
+ #divide = (dividend, divisor, precision = 19, calculatedRound = false) => {
384
+ dividend = dividend + ''
385
+ divisor = divisor + ''
386
+
387
+ if (dividend.trim().length === 0 || divisor.trim().length === 0) {
388
+ return '0'
389
+ }
390
+
391
+ if (this.#compare(divisor, '0') === 0) {
392
+ throw new Error('dividend can not be 0')
393
+ }
394
+
395
+ if (this.#compare(dividend, '0') === 0) {
396
+ return '0'
397
+ }
398
+
399
+ let isNegative = false
400
+ if (dividend[0] === '-') {
401
+ if (divisor[0] !== '-') {
402
+ isNegative = true
403
+ dividend = dividend.slice(1)
404
+ } else {
405
+ isNegative = false
406
+ dividend = dividend.slice(1)
407
+ divisor = divisor.slice(1)
408
+ }
409
+ } else if (divisor[0] === '-') {
410
+ isNegative = true
411
+ divisor = divisor.slice(1)
412
+ }
413
+
414
+ let decimalIndex = divisor.match(/\.(\d+)/)
415
+ if (decimalIndex) {
416
+ dividend = this.#multiply(dividend, '1' + '0'.repeat(decimalIndex[1].length))
417
+ divisor = divisor.replace('.', '')
418
+ }
419
+
420
+ if (!dividend.includes('.')) dividend += '.0'
421
+
422
+ let result = ''
423
+ let curDividend = ''
424
+ let totalLength = dividend.match(/\d+/)[0].length + precision + 1
425
+ for (let i = 0; i <= totalLength; i++) {
426
+ if (dividend[i] === '.') {
427
+ result += '.'
428
+ continue
429
+ }
430
+
431
+ if (dividend[i] === undefined) {
432
+ curDividend += '0'
433
+ } else {
434
+ curDividend += dividend[i]
435
+ }
436
+
437
+ if (this.#compare(curDividend, divisor) === 1 || this.#compare(curDividend, divisor) === 0) {
438
+ let n = '1'
439
+ while (this.#compare(this.#multiply(divisor, n.toString()), curDividend) === -1) {
440
+ n = this.#plus(n, '1')
441
+ }
442
+
443
+ if (this.#compare(curDividend, divisor) === 0) {
444
+ result += n
445
+ curDividend = '0'
446
+ } else {
447
+ result += this.#minus(n, '1')
448
+ curDividend = this.#minus(curDividend, this.#multiply(divisor, this.#minus(n, '1')))
449
+ }
450
+ } else {
451
+ result += '0'
452
+ }
453
+ }
454
+
455
+ result = result.replace(/^0+/, '')
456
+ result = result.replace(/0+$/, '')
457
+
458
+ if (result.length === 0 || result === '.' || result === '0.' || result === '.0') result = '0'
459
+
460
+ if (result[0] === '.' && result !== '0') result = '0' + result
461
+ if (result[result.length - 1] === '.' && result !== '0') result += '0'
462
+
463
+ if (calculatedRound) {
464
+ result = this.#round(result, precision)
465
+ } else {
466
+ let decimalLength = result.match(/\.(\d+)/)[1].length
467
+ if (this.#compare(decimalLength, precision) === 1) {
468
+ result = this.#round(result, precision)
469
+ }
470
+ }
471
+
472
+ if (isNegative && result !== '0') {
473
+ result = '-' + result
474
+ }
475
+
476
+ return result
477
+ }
478
+
479
+ #thousandSeparator = (amount) => {
480
+ if (amount[0] === '-') {
481
+ return '-' + this.#thousandSeparator(amount.slice(1))
482
+ }
483
+
484
+ if (amount.includes('.')) {
485
+ let integerPart = amount.split('.')[0]
486
+ let decimalPart = amount.split('.')[1]
487
+ decimalPart = decimalPart.split('').reverse().join('')
488
+ decimalPart = decimalPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
489
+ decimalPart = decimalPart.split('').reverse().join('')
490
+ return integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + '.' + decimalPart
491
+ } else {
492
+ return amount.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
493
+ }
494
+ }
495
+
496
+ #translateAmount = (amount) => {
497
+ let isNegative = false;
498
+ if (amount.length !== 0 && amount[0] === "-") {
499
+ isNegative = true;
500
+ amount = amount.slice(1);
501
+ }
502
+
503
+ amount = amount.replace(/^0+/, "")
504
+
505
+ if (amount === "") return "零元整"
506
+ if (amount[0] === '.') amount = '0' + amount
507
+ if (amount[amount.length - 1] === '.') amount += '0'
508
+
509
+ let amountList = (amount + "").split("\.");
510
+ let integer = amountList[0];
511
+ let decimal = amountList.length > 1 ? amountList[1] : "";
512
+
513
+ let groups = integer.match(/\d{1,4}(?=(\d{4})*$)/g) || []
514
+
515
+ let list = [];
516
+ for (let am of groups) {
517
+ let de = am + "";
518
+
519
+ de = de.replace(/^0+/, "");
520
+
521
+ let s = this.#convertChinese(de)
522
+ if ((de + "").length < am.length) {
523
+ s = "零" + s;
524
+ }
525
+ list = [...list, s];
526
+ }
527
+ amountList = list;
528
+ let integerPart = this.#integerPartToChinese(amountList);
529
+
530
+ let decimalPart = !(decimal.length === 0) ? this.#decimalPartToChinese(decimal) : "";
531
+
532
+ let chineseAmount = integerPart + decimalPart;
533
+
534
+ let remainder = (Number(amount) % 1) + ""
535
+ if ("0" === remainder) {
536
+ chineseAmount += "整";
537
+ }
538
+
539
+ if (decimalPart.indexOf("角") !== -1 && decimalPart.indexOf("分") === -1) {
540
+ chineseAmount += "整";
541
+ }
542
+
543
+ return (isNegative ? "负" : "") + chineseAmount;
544
+ }
545
+
546
+ #integerPartToChinese = (integerPart) => {
547
+ let needZero = false;
548
+ let units = [ "亿", "万", "元" ];
549
+ if (integerPart.length > 3) return "";
550
+ for (let i = 0; i < integerPart.length; i++) {
551
+ if ("零" === integerPart[i]) {
552
+ needZero = true;
553
+ continue;
554
+ }
555
+
556
+ if (needZero && integerPart[i][0] !== "零") {
557
+ integerPart[i] = "零" + integerPart[i];
558
+ }
559
+ let index = units.length - integerPart.length + i;
560
+ integerPart[i] = integerPart[i] + units[index];
561
+ }
562
+
563
+ let list = integerPart;
564
+
565
+ list = list.filter(s => s !== "零");
566
+
567
+ let s = list[list.length - 1];
568
+ if (s[s.length - 1] !== "元") {
569
+ list.push("元");
570
+ }
571
+
572
+ return list.join("");
573
+ }
574
+
575
+ #convertChinese = (amount) => {
576
+ let chinese = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
577
+ let unit = ["仟", "佰", "拾", ""];
578
+ let amountStr = amount + "";
579
+ let chineseAmount = "";
580
+ let isZero = false;
581
+ for (let i = 0; i < amountStr.length; i++) {
582
+ if (amountStr[i] - '0' == 0) {
583
+ isZero = true;
584
+ continue;
585
+ }
586
+ let index = unit.length - (amountStr.length - i);
587
+ if (isZero) {
588
+ chineseAmount += "零";
589
+ isZero = false;
590
+ }
591
+ chineseAmount += chinese[amountStr[i] - '0'] + unit[index];
592
+ }
593
+
594
+ return chineseAmount;
595
+ }
596
+
597
+ #decimalPartToChinese = (decimalPart) => {
598
+ let chinese = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
599
+ let unit = ["角", "分"];
600
+ let chineseDecimal = "";
601
+ let isZero = false;
602
+ for (let i = 0; i < decimalPart.length; i++) {
603
+ if (decimalPart[i] - '0' == 0) {
604
+ isZero = true;
605
+ continue;
606
+ }
607
+
608
+ let s = chinese[decimalPart[i] - '0'] + unit[i];
609
+ if (isZero) {
610
+ chineseDecimal += "零";
611
+ }
612
+ chineseDecimal += s;
613
+ }
614
+
615
+ return chineseDecimal;
616
+ }
617
+
618
+ set = (num) => {
619
+ this.#amount = num
620
+ }
621
+
622
+ get = () => {
623
+ return this.#amount
624
+ }
625
+
626
+ compare = (num) => {
627
+ return this.#compare(this.#amount, num)
628
+ }
629
+
630
+ toFixed = (precision) => {
631
+ return this.#round(this.#amount, precision)
632
+ }
633
+
634
+ add = (num) => {
635
+ this.#amount = this.#plus(this.#amount, num)
636
+ return this
637
+ }
638
+
639
+ subtract = (num) => {
640
+ this.#amount = this.#minus(this.#amount, num)
641
+ return this
642
+ }
643
+
644
+ multiply = (num) => {
645
+ this.#amount = this.#multiply(this.#amount, num)
646
+ return this
647
+ }
648
+
649
+ divide = (num, precision = 19, calculatedRound = false) => {
650
+ this.#amount = this.#divide(this.#amount, num, precision, calculatedRound)
651
+ return this
652
+ }
653
+
654
+ translateAmount = () => {
655
+ return this.#translateAmount(this.#amount)
656
+ }
657
+
658
+ toLocaleString = () => {
659
+ return this.#thousandSeparator(this.#amount)
660
+ }
661
+
662
+ toString = () => {
663
+ return this.#amount
664
+ }
665
+ }
666
+
667
+ module.exports = AmountUtil
668
+
669
+
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # AmountUtil
2
+
3
+ The util that can help you to manage about amount,we have introduced a very powerful Chinese amount translation feature.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install amountutil
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ const AmountUtil = require('amountutil');
15
+
16
+ // 转换为中文金额
17
+ let amount = new AmountUtil(123456.78);
18
+ const chineseAmount = amount.translateAmount();
19
+ console.log(chineseAmount); // 输出:"壹拾贰万叁仟肆佰伍拾陆元柒角捌分"
20
+
21
+ // 大数加减乘除
22
+ let amount1 = new AmountUtil(123456.78);
23
+ let sum = amount1.add(98765.43);
24
+ console.log(sum.toString()); // 输出:222222.21
25
+
26
+ let diff = amount1.subtract(98765.43);
27
+ console.log(diff.toString()); // 输出:123456.78
28
+
29
+ let product = amount1.multiply(2);
30
+ console.log(product.toString()); // 输出:246913.56
31
+
32
+ let quotient = amount1.divide(2, precision = 2);
33
+ console.log(quotient.toString()); // 输出:123456.78
34
+
35
+ // 比较、保留小数位数、千分符
36
+ console.log(amount1.compare(98765.43)); // 输出:1
37
+ console.log(amount1.toFixed(1)); // 输出:123456.8
38
+ console.log(amount1.toLocaleString()); // 输出:123,456.78
39
+ ```
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "amountutil",
3
+ "version": "1.0.0",
4
+ "description": "The util that can help you to manage about amount,we have introduced a very powerful Chinese amount translation feature.",
5
+ "main": "AmountUtil.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "金额转换中文",
11
+ "金额转换",
12
+ "中文金额",
13
+ "金额",
14
+ "金额工具"
15
+ ],
16
+ "author": "ZhuYngve",
17
+ "license": "ISC"
18
+ }