-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberUtils.ts
94 lines (75 loc) · 2.93 KB
/
NumberUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
export default class NumberUtils {
public formatMoney = (value: any) => {
const options = {
style: 'currency',
currency: 'BDT',
minimumFractionDigits: 2,
}
/* if (Number(value) % 100 === 0) {
options.minimumFractionDigits = 0
}*/
const formatter = new Intl.NumberFormat('en-IN', options)
return formatter.format(this.round(this.toNumber(value), 2))
}
public formatNumber = (value: any, trimTrailingZeroes:boolean = true, digitCount: number = 2) => {
if(value === null || value === undefined || value === '') return 0;
const options = {
style: 'decimal',
minimumFractionDigits: digitCount,
}
if (trimTrailingZeroes) {
options.minimumFractionDigits = 0
}
/*if (Number(value) % 100 === 0) {
options.minimumFractionDigits = 0
}*/
const formatter = new Intl.NumberFormat('en-IN', options)
return formatter.format(this.round(this.toNumber(value), digitCount))
}
public formatNumberInteger = (value: any) => {
const options = {
style: 'decimal',
minimumFractionDigits: 0,
}
/*if (Number(value) % 100 === 0) {
options.minimumFractionDigits = 0
}*/
const formatter = new Intl.NumberFormat('en-IN', options)
return formatter.format(this.round(this.toNumber(value), 0))
}
public formatPercentage = (value: number): string => {
const options = {
style: 'percent',
minimumFractionDigits: 1,
}
const formatter = new Intl.NumberFormat('en-IN', options)
return formatter.format(this.round(this.toNumber(value), 2))
}
public toNumber = (value: any): number => {
if(value === null || value === undefined || value === '') return 0;
else if (typeof value === 'string') {
value = value.replace(',', '');
}
return Number(value);
}
public round = (amount: number, digitCount: number): number => {
let precision = Math.pow(10, digitCount);
return Math.round(amount * precision) / precision;
}
public roundUp = (amount: number, digitCount: number): number => {
let precision = Math.pow(10, digitCount);
return Math.ceil(amount * precision) / precision;
}
public roundDown = (amount: number, digitCount: number): number => {
let precision = Math.pow(10, digitCount);
return Math.floor(amount * precision) / precision;
}
public roundTruncate = (amount: number, digitCount: number): number => {
let precision = Math.pow(10, digitCount);
return Math.trunc(amount * precision) / precision;
}
public toFixed = (number: any): string => {
if (number === null || number === undefined || number === "") { return ''}
return Number(number).toFixed(2);
}
}