-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelpers.php
More file actions
47 lines (38 loc) · 1.27 KB
/
helpers.php
File metadata and controls
47 lines (38 loc) · 1.27 KB
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
<?php
function numberToChinese($number) {
$digits = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
$tens = ['', '十', '百', '千'];
$bigNumbers = ['', '万', '亿', '兆'];
if ($number == 0) {
return '零';
}
$negative = $number < 0 ? '负' : '';
$number = abs($number);
$parts = [];
$partIndex = 0;
while ($number > 0) {
$part = '';
$currentPart = $number % 10000;
$number = intdiv($number, 10000);
if ($currentPart > 0) {
$tempPart = '';
$digitIndex = 0;
while ($currentPart > 0) {
$digit = $currentPart % 10;
if ($digit > 0) {
$tempPart = $digits[$digit] . ($digitIndex > 0 ? $tens[$digitIndex] : '') . $tempPart;
} else if (strlen($tempPart) > 0 && !str_ends_with($tempPart, '零')) {
$tempPart = '零' . $tempPart;
}
$currentPart = intdiv($currentPart, 10);
$digitIndex++;
}
$part = $tempPart . $bigNumbers[$partIndex];
}
if ($part != '') {
array_unshift($parts, $part);
}
$partIndex++;
}
return $negative . implode('', $parts);
}