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
|
//PHP人民币金额转大写 class Ext_Num2Cny{ static $basical = array (0=> "零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" ); static $advanced = array (1=> "拾" , "佰" , "仟" ); public static function ParseNumber( $number ){ $number =trim( $number ); if ( $number >999999999999) return "数字太大,无法处理。抱歉!" ; if ( $number ==0) return "零" ; if ( strpos ( $number , '.' )){ $number = round ( $number ,2); $data = explode ( "." , $number ); $data [0]=self::int( $data [0]); $data [1]=self::dec( $data [1]); return $data [0]. $data [1]; } else { return self::int( $number ). '整' ; } } public static function int( $number ){ $arr = array_reverse ( str_split ( $number )); $data = '' ; $zero =false; $zero_num =0; foreach ( $arr as $k => $v ){ $_chinese = '' ; $zero =( $v ==0)?true:false; $x = $k %4; if ( $x && $zero && $zero_num >1) continue ; switch ( $x ){ case 0: if ( $zero ){ $zero_num =0; } else { $_chinese =self:: $basical [ $v ]; $zero_num =1; } if ( $k ==8){ $_chinese .= '亿' ; } elseif ( $k ==4){ $_chinese .= '万' ; } break ; default : if ( $zero ){ if ( $zero_num ==1){ $_chinese =self:: $basical [ $v ]; $zero_num ++; } } else { $_chinese =self:: $basical [ $v ]; $_chinese .=self:: $advanced [ $x ]; } } $data = $_chinese . $data ; } return $data . '元' ; } public static function dec( $number ){ if ( strlen ( $number )<2) $number .= '0' ; $arr = array_reverse ( str_split ( $number )); $data = '' ; $zero_num =false; foreach ( $arr as $k => $v ){ $zero =( $v ==0)?true:false; $_chinese = '' ; if ( $k ==0){ if (! $zero ){ $_chinese =self:: $basical [ $v ]; $_chinese .= '分' ; $zero_num =true; } } else { if ( $zero ){ if ( $zero_num ){ $_chinese =self:: $basical [ $v ]; } } else { $_chinese =self:: $basical [ $v ]; $_chinese .= '角' ; } } $data = $_chinese . $data ; } return $data ; } } //使用方法 $nun = new Ext_Num2Cny(); echo Ext_Num2Cny::ParseNumber(1234567.5); |
结果:壹佰贰拾叁万肆仟伍佰陆拾柒元伍角
希望本文对大家学习php编程有所帮助,