-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JapaneseNumerals.php
244 lines (218 loc) · 7.77 KB
/
JapaneseNumerals.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
class JapaneseNumerals
{
const USE_FORMAL = 1;
const USE_FORMAL_TEN_THOUSAND = 2;
/**
@return string Japanese Numeral
*/
public static function to($left, $right = null, $flags = 0)
{
$numbers = self::getNumbers($flags);
$zero = self::getZero($flags);
$quartets = self::getQuartets($flags);
$string = '';
$data_array = [];
$data_string = '';
$decimal_string = '';
if ($right != null) {
$decimal_string = $right;
}
$t = (ceil(strlen($left) / 4) * 4) - strlen($left); // Amount of placeholder we're going to need to make the quartets evenly divisible. This makes it so much easier on us.
$left = str_repeat('0', $t).$left; // Prepend Zeros to make an Even Amount
// Make an Array of Each Quartet
$i = 4;
while ($i <= strlen($left)) {
$data_array[] = substr($left, $i * -1, 4);
$i += 4;
}
// Create the Number from the lowest quartet to the highest
foreach ($data_array as $i => $v) {
if (intval($v) == 0) {
continue;
} // Skip the Quartet if its worthless
$temp_string = '';
// Each of the below "Place"s are per-quartet places
// Ones Place
$temp_string = $numbers[substr($v, -1, 1)];
// Tens Place
if (substr($v, -2, 1) == 1) { // If we have --1-
$temp_string = $numbers[10].$temp_string;
} elseif (substr($v, -2, 1) != 0) { // If we have --X-
$temp_string = $numbers[substr($v, -2, 1)].$numbers[10].$temp_string;
}
// Hundreds Place
if (substr($v, -3, 1) == 1) { // If we have -1--
$temp_string = $numbers[100].$temp_string;
} elseif (substr($v, -3, 1) != 0) { // If we have -X--
$temp_string = $numbers[substr($v, -3, 1)].$numbers[100].$temp_string;
}
// Thousands Place
if (substr($v, -4, 1) == 1) { // If we have 1---
$temp_string = $numbers[1000].$temp_string;
} elseif (substr($v, -4, 1) != 0) { // If we have X---
$temp_string = $numbers[substr($v, -4, 1)].$numbers[1000].$temp_string;
}
// Prepend the newest data to string, separating it from the string by the quartet identifier
$data_string = $temp_string.$quartets[$i].$data_string;
}
// If there is no number, our number is zero - of course!
if ($data_string == '') {
$data_string = $zero;
}
// Now we do the decimal (if we have one)
if ($decimal_string != '') {
$decimal_convert = $numbers;
$decimal_convert[0] = $zero; // Replace the placeholder with Zero here.
$decimal_string = str_replace([
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'x',
], // Each number to be replaced. X is a placeholder for the non-existant 10
$decimal_convert, $decimal_string);
// Separate the Left and Right of the decimal using a Japanese Middot
$data_string = $data_string.'・'.$decimal_string;
}
// Finally, Return the number!
return $data_string;
}
/**
@return string Arabic Numeral
*/
public static function from($num)
{
$definitionTable = self::getDefinitionTables();
$numbers = $definitionTable['numbers'];
$myriads = $definitionTable['myriads'];
$len = mb_strlen($num);
$myriad = 0;
$temp = 1;
$temp2 = 0;
$result = '0';
mb_internal_encoding('UTF-8');
for ($i = 0;$i < $len;++$i) {
$parse = self::substr_unicode($num, $i, 1);
if (isset($numbers[$parse]) && intval($numbers[$parse]) < 10) {
$temp = intval($numbers[$parse]);
$temp2 = $temp;
} elseif (isset($numbers[$parse])) {
$myriad += $temp * intval($numbers[$parse]);
$temp = 1;
$temp2 = 0;
} else {
foreach ($myriads as $j => $v) {
if (self::substr_unicode($j, 0, 1) == $parse) {
$i += mb_strlen($j);
if ($myriad == 0) {
$myriad = $temp;
}
$result = bcadd($result, bcmul($myriad, $v));
$myriad = 0;
$temp = 1;
$temp2 = 0;
break;
}
}
}
}
$result = bcadd($result, $temp2);
$result = bcadd($result, $myriad);
return $result;
}
public static function getDefinitionTables()
{
$temp = self::getNumbers();
$temp[0] = self::getZero();
$numbers = [];
foreach ($temp as $arabic => $kanji) {
$numbers[$kanji] = $arabic;
}
$numbers['壱'] = '1';
$numbers['弐'] = '2';
$numbers['参'] = '3';
$numbers['拾'] = '10';
$numbers[self::getZero(1)] = '0';
$temp = self::getQuartets();
$quartets = [];
foreach ($temp as $key => $kanji) {
if ($kanji == '') {
continue;
}
$quartets[$kanji] = bcpow(10, $key * 4);
}
$quartets['萬'] = '10000';
return ['numbers' => $numbers, 'myriads' => $quartets];
}
protected static function getNumbers($flags = 0)
{
$numbers = [
'', // Blank for ease of use. Going to use Array Index to grab the proper character
'一', // 1
'二', // 2
'三', // 3
'四', // 4
'五', // 5
'六', // 6
'七', // 7
'八', // 8
'九', // 9
'十', // 10
];
if ($flags & self::USE_FORMAL == self::USE_FORMAL) {
$numbers[1] = '壱'; // Formal 1
$numbers[2] = '弐'; // Formal 2
$numbers[3] = '参'; // Formal 3
$numbers[10] = '拾'; // Formal 10
}
$numbers[100] = '百'; // 100
$numbers[1000] = '千'; // 1,000
return $numbers;
}
protected static function getZero($flags = 0)
{
if ($flags & self::USE_FORMAL == self::USE_FORMAL) {
return '零';
} // Formal 0
return '〇'; // Informal 0
}
protected static function getQuartets($flags = 0)
{
// Adding to this array should have no negative consequences
$quartets = [
'', // Default Quartet has no symbol
'万', // 10^4
'億', // 10^8
'兆', // 10^12
'京', // 10^16
'垓', // 10^20
'秭', // 10^24
'穣', // 10^28
'溝', // 10^32
'澗', // 10^36
'正', // 10^40
'載', // 10^44
'極', // 10^48
'恒河沙', // 10^52
'阿僧祇', // 10^56
'那由他', // 10^60
'不可思議', // 10^64
'無量大数', // 10^68 (as high as quartets currently go?)
];
if ($flags & self::USE_FORMAL_TEN_THOUSAND == self::USE_FORMAL_TEN_THOUSAND) {
$quartets[1] = '萬';
} // Older formal character for 10,000
return $quartets;
}
final protected static function substr_unicode($str, $s, $l = null)
{
return implode('', array_slice(preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}
}