-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
193 lines (172 loc) · 4.65 KB
/
index.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const NumberAssistent = function() {};
// این تابع عدد انگلیسی ورودی را به صورت فارسی برمیگرداند
NumberAssistent.prototype.number = function(input) {
if (input || input === 0) {
input = String(input);
if (input && input.length) {
return input.replace(/\d/g, function(x) {
return String.fromCharCode(x.charCodeAt(0) + 1728);
});
}
return "";
}
return input;
};
// این تابع عدد فارسی ورودی را به صورت انگلیسی برمیگرداند
NumberAssistent.prototype.Enumber = (input) => {
if (input) {
input = String(input);
if (input && input.length) {
input = input.replace(/,/g, '');
return input.replace(/[۰-۹]/g, function(x) {
return String.fromCharCode(x.charCodeAt(0) - 1728);
});
}
return '';
}
return input;
}
// این تابع تمامی اعدادی که در ورودی باشند را سه رقم سه رقم با علامت
// ,
// از هم جدا میکند
NumberAssistent.prototype.ESeparator = function(input, Decimals = 3) {
let Input = String(input);
return Input.replace(/\d+(\.\d+)?/g, function(x) {
const t = ",",
s = x < 0 ? "-" : "";
let j = null,
i = String(parseInt((n = Math.abs(Number(x) || 0))));
(j = (j = i.length) > 3 ? j % 3 : 0),
(decimal =
parseFloat(x) - parseInt(x) > 0
? String(
Math.round((parseFloat(x) - parseInt(x)) *( 10**Decimals )) / (10**Decimals)
)
: "");
return (
(j ? i.substr(0, j) + t : "") +
i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
s +
decimal.slice(1)
);
});
};
// این تابع تمامی اعدادی که در ورودی باشند را سه رقم سه رقم با علامت
// ,
// از هم جدا میکند و به صورت فارسی بر میگرداند
NumberAssistent.prototype.PSeparator = function (input, Decimals = 3){
return this.number(this.ESeparator(input, Decimals));
}
// عدد را به حروف بر میگرداند
NumberAssistent.prototype.word = function(value) {
let delimiter,
valueParts,
digit,
i,
iThree,
numbers,
parts,
result,
resultThree,
three;
if (!isFinite(value)) {
return 'عدد اشتباه است';
}
value = String( Math.floor( Math.abs(value) ) );
if (value.length > 24) {
return 'عدد بسیار بزرگ است';
}
parts = [
'',
'هزار',
'میلیون',
'میلیارد',
'تریلیون',
'کوادریلیون',
'کویینتیلیون',
'سکستیلیون'
];
numbers = {
0: [
'',
'صد',
'دویست',
'سیصد',
'چهارصد',
'پانصد',
'ششصد',
'هفتصد',
'هشتصد',
'نهصد'
],
1: ['', 'ده', 'بیست', 'سی', 'چهل', 'پنجاه', 'شصت', 'هفتاد', 'هشتاد', 'نود'],
2: ['', 'یک', 'دو', 'سه', 'چهار', 'پنج', 'شش', 'هفت', 'هشت', 'نه'],
two: [
'ده',
'یازده',
'دوازده',
'سیزده',
'چهارده',
'پانزده',
'شانزده',
'هفده',
'هجده',
'نوزده'
],
zero: 'صفر'
};
delimiter = ' و ';
valueParts = value
.split('')
.reverse()
.join('')
.replace(/\d{3}(?=\d)/g, '$&,')
.split('')
.reverse()
.join('')
.split(',')
.map(function(str) {
return Array(4 - str.length).join('0') + str;
});
result = (function() {
var _results;
_results = [];
const calc =() =>{
var _i, _len, _results1;
_results1 = [];
for (i = _i = 0, _len = three.length; _i < _len; i = ++_i) {
digit = three[i];
if (i === 1 && digit === '1') {
_results1.push(numbers.two[three[2]]);
} else if (
(i !== 2 || three[1] !== '1') &&
numbers[i][digit] !== ''
) {
_results1.push(numbers[i][digit]);
} else {
continue;
}
}
return _results1;
}
for (iThree in valueParts) {
three = valueParts[iThree];
resultThree = calc();
resultThree = resultThree.join(delimiter);
if (!!resultThree)
_results.push(
resultThree + ' ' + parts[valueParts.length - iThree - 1]
);
}
return _results;
})();
result = result.filter(function(x) {
return x.trim() !== '';
});
result = result.join(delimiter).trim();
if (result === '') {
result = numbers.zero;
}
return result;
}
module.exports = NumberAssistent;