文章来源:IT工程信息网 http://www.systhinker.com/html/09/n-12309.html
之前用Access作一个应用,但找不到货币转换成大写人民币的功能(当然可以通过excel 转换,但不在一个应用上,放弃)。以为比较简单,自己试写,谁知用了两个晚上,才基本实现,但没有小数点(元)以下货币的转换。也没有兆的判断。而且对字符的合法性也没有检验。今天有时间,将其完善一下。(可能还会有考虑不周的地方)
思路:
1、判断是否带有小数点的金额,如果是,就找出小数点所在数据。该位字符不进行转换。
2、小数点前的作为整数位,进行转换。整数位的每一位都有表示金额的级别:拾、佰、仟。而且还有万、亿的级别。
3、小数点后的作为角、分、厘处理。
4、如果有连续的零,只显示一个零。如50006,显示为伍万零陆。如果是发生在拾、佰、仟位,该零还要去掉。
000 | function TForm1 . changeRmb( const strRmb: string ): string ; |
001 | var |
002 | txt,strhighlevel: string ; |
003 | i,n,m,ilen,ipos: Integer ; //n记录整数部分长度,m记录分数部分长度 |
004 | strarray,strlevel: array of string ; |
005 | p: pchar ; |
006 | ispoint: boolean ; //判断是否有小数点 |
007 | begin |
008 | ispoint:= false ; |
009 | result:= '' ; |
010 | ipos:= 0 ; |
011 | m:= 0 ; |
012 | txt:=Trim(strRmb); |
013 | i:= 1 ; |
014 | p:= PChar (txt); |
015 | //去除开头的0,以及. |
016 | if ((txt[ 1 ]= '0' ) and (txt[ 2 ]<> '.' )) or (txt[ 1 ]= '.' ) then |
017 | begin |
018 | ShowMessage( '第1位不能为0或者是.,退出操作' ); |
019 | exit; |
020 | end ; |
021 | //检查字符的合法性 |
022 | while (i<length(txt)) do |
023 | begin |
024 | if (p^> '9' ) or ((p^< '0' ) and (P^<> '.' )) then //ord('.')=46 |
025 | begin |
026 | ShowMessage( PChar ( '第' +inttostr(i)+ '位包含非数字字符,将退出操作' )); |
027 | Exit; |
028 | end ; |
029 | if P^= '.' then |
030 | if ispoint then |
031 | begin |
032 | showmessage( '太多小数点,将退出!' ); |
033 | exit; |
034 | end |
035 | else |
036 | begin |
037 | ipos:=i; |
038 | ispoint:= true ; |
039 | end ; |
040 | Inc(p); |
041 | Inc(i); |
042 | end ; //while |
043 | ilen:=Length(txt); |
044 | if ispoint then |
045 | begin |
046 | n:=ipos- 1 ; |
047 | m:=ilen-ipos; |
048 | end |
049 | else |
050 | n:=ilen; |
051 | //判断是否超过万,或亿 |
052 | if m> 3 then |
053 | begin |
054 | ShowMessage( '小数点后位数超过3,无法转换!' ); |
055 | Exit; |
056 | end ; |
057 | SetLength(strarray,ilen+ 8 ); |
058 | SetLength(strlevel,ilen+ 8 ); |
059 | for i:=iLen downto 1 do |
060 | begin |
061 | if txt[i]<> '.' then |
062 | case strtoint(txt[i]) of |
063 | 1 :strarray[i]:= '壹' ; |
064 | 2 :strarray[i]:= '贰' ; |
065 | 3 :strarray[i]:= '叁' ; |
066 | 4 :strarray[i]:= '肆' ; |
067 | 5 :strarray[i]:= '伍' ; |
068 | 6 :strarray[i]:= '陆' ; |
069 | 7 :strarray[i]:= '柒' ; |
070 | 8 :strarray[i]:= '捌' ; |
071 | 9 :strarray[i]:= '玖' ; |
072 | 0 : |
073 | begin |
074 | strarray[i]:= '零' ; |
075 | if i<ilen then //如果低位也为零,低位零不显示 |
076 | if (strarray[i+ 1 ]= '' ) or (strarray[i+ 1 ]= '零' ) then |
077 | begin |
078 | //strarray[i+1]:= ''; |
079 | strarray[i]:= '' ; |
080 | end ; |
081 | if i=n then strarray[i]:= '' ; |
082 | strlevel[i]:= '' ; |
083 | end ; |
084 | end ; //case |
085 | end ; |
086 | //先处理 小数点部分 |
087 | if m> 0 then |
088 | begin |
089 | for i:=m downto 1 do |
090 | begin |
091 | strlevel[ipos+i]:= '' ; |
092 | case i- 1 of |
093 | 0 : |
094 | if txt[ipos+i]= '0' then |
095 | strarray[ipos+i]:= '' |
096 | else |
097 | strlevel[ipos+i]:= '角' ; |
098 | 1 : |
099 | if txt[ipos+i]= '0' then |
100 | strarray[ipos+i]:= '' |
101 | else |
102 | strlevel[ipos+i]:= '分' ; |
103 | 2 : |
104 | if txt[ipos+i]= '0' then |
105 | strarray[ipos+i]:= '' |
106 | else strlevel[ipos+i]:= '厘' ; |
107 | end ; |
108 | Result:=strarray[ipos+i]+strlevel[ipos+i]+result; |
109 | end ; |
110 | end ; |
111 | if ispoint and (txt[ipos- 1 ]= '0' ) and (n= 1 ) then |
112 | Result:=result+ '' //如果少于1块时,不要显示元。 |
113 | else |
114 | Result:= '元' +result; |
115 | for i:=n downto 1 do |
116 | begin |
117 | case n-i of |
118 | 0 , 4 , 8 , 12 : strlevel[i]:= '' ; |
119 | 1 , 5 , 9 , 13 : strlevel[i]:= '拾' ; |
120 | 2 , 6 , 10 , 14 : strlevel[i]:= '佰' ; |
121 | 3 , 7 , 11 , 15 : strlevel[i]:= '仟' ; |
122 | end ; //case |
123 | if (txt[i]= '0' ) then strlevel[i]:= '' ; |
124 | //要处理零 以及加上万、亿 |
125 | if n-i= 4 then |
126 | begin |
127 | if strarray[i]= '零' then strarray[i]:= '' ; |
128 | Result:=strarray[i]+strlevel[i]+ '万' +result |
129 | end |
130 | else if n-i= 8 then |
131 | begin |
132 | if strarray[i]= '零' then strarray[i]:= '' ; |
133 | Result:=strarray[i]+strlevel[i]+ '亿' +result |
134 | end //begin |
135 | else if n-i= 12 then |
136 | begin |
137 | if strarray[i]= '零' then strarray[i]:= '' ; |
138 | Result:=strarray[i]+strlevel[i]+ '兆' +result |
139 | end //begin |
140 | else |
141 | Result:=strarray[i]+strlevel[i]+result; |
142 | end ; //for |
143 | end ; |
文章来源:IT工程信息网 http://www.systhinker.com/html/09/n-12309.html