File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed
Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -8,17 +8,28 @@ function numberToChinese($number)
88 $ chars = [];
99 $ number = strval ($ number ); // 确保 number 是字符串类型
1010 $ length = strlen ($ number );
11+ $ zeroFlag = false ; // 标记是否连续零
12+
1113 for ($ i = 0 ; $ i < $ length ; $ i ++) {
1214 $ n = $ number [$ length - $ i - 1 ];
13- $ chars [] = $ units [$ i ];
14- $ chars [] = $ numbers [$ n ];
15+ if ($ n == "0 " ) {
16+ if (!$ zeroFlag ) {
17+ $ chars [] = $ numbers [$ n ];
18+ $ zeroFlag = true ;
19+ }
20+ } else {
21+ $ zeroFlag = false ;
22+ $ chars [] = $ units [$ i ];
23+ $ chars [] = $ numbers [$ n ];
24+ }
1525 }
1626 $ chars = array_reverse ($ chars );
1727 $ chars = implode ('' , $ chars );
1828 $ chars = preg_replace ('/零[十百千]/u ' , '零 ' , $ chars );
1929 $ chars = preg_replace ('/零+/u ' , '零 ' , $ chars );
2030 $ chars = rtrim ($ chars , '零 ' );
21- $ chars = preg_replace ('/零$/u ' , '' , $ chars );
31+ $ chars = preg_replace ('/零万/u ' , '万 ' , $ chars );
32+ $ chars = preg_replace ('/零亿/u ' , '亿 ' , $ chars );
2233 $ chars = str_replace ('一十 ' , '十 ' , $ chars );
2334 return $ chars ;
2435 }
You can’t perform that action at this time.
0 commit comments