數字轉換大寫人民幣的delphi實現

2022-05-13 05:17:37 字數 3179 閱讀 2780

function tform1.changermb(const strrmb:string):string;

var txt,strhighlevel:string;

i,n,m,ilen,ipos:integer; //n記錄整數部分長度,m記錄分數部分長度

strarray,strlevel:array of string;

p:pchar;

ispoint:boolean;//判斷是否有小數點

begin

ispoint:=false;

result:='';

ipos:=0;

m:=0;

txt:=trim(strrmb);

i:=1;

p:=pchar(txt);

//去除開頭的0,以及.

if ((txt[1]='0') and (txt[2]<>'.')) or (txt[1]='.') then

begin

showmessage('第1位不能為0或者是.,退出操作');

exit;

end;

//檢查字元的合法性

while (i'9') or ((p^<'0') and (p^<>'.')) then //ord('.')=46

begin

showmessage(pchar('第'+inttostr(i)+'位包含非數字字元,將退出操作'));

exit;

end;

if p^='.' then

if ispoint then

begin

showmessage('太多小數點,將退出!');

exit;

endelse

begin

ipos:=i;

ispoint:=true;

end;

inc(p);

inc(i);

end;//while

ilen:=length(txt);

if ispoint then

begin

n:=ipos-1;

m:=ilen-ipos;

endelse

n:=ilen;

//判斷是否超過萬,或億

if m>3 then

begin

showmessage('小數點後位數超過3,無法轉換!');

exit;

end;

setlength(strarray,ilen+8);

setlength(strlevel,ilen+8);

for i:=ilen downto 1 do

begin

if txt[i]<>'.' then

case strtoint(txt[i]) of

1:strarray[i]:='壹';

2:strarray[i]:='貳';

3:strarray[i]:='叄';

4:strarray[i]:='肆';

5:strarray[i]:='伍';

6:strarray[i]:='陸';

7:strarray[i]:='柒';

8:strarray[i]:='捌';

9:strarray[i]:='玖';

0:begin

strarray[i]:='零';

if i0 then

begin

for i:=m downto 1 do

begin

strlevel[ipos+i]:='';

case i-1 of

0:if txt[ipos+i]='0' then

strarray[ipos+i]:=''

else

strlevel[ipos+i]:='角';

1:if txt[ipos+i]='0' then

strarray[ipos+i]:=''

else

strlevel[ipos+i]:='分';

2:if txt[ipos+i]='0' then

strarray[ipos+i]:=''

else strlevel[ipos+i]:='釐';

end;

result:=strarray[ipos+i]+strlevel[ipos+i]+result;

end;

end;

if ispoint and (txt[ipos-1]='0') and (n=1) then

result:=result+'' //如果少於1塊時,不要顯示元。

else

result:='元'+result;

for i:=n downto 1 do

begin

case n-i of

0,4,8,12: strlevel[i]:='';

1,5,9,13: strlevel[i]:='拾';

2,6,10,14: strlevel[i]:='佰';

3,7,11,15: strlevel[i]:='仟';

end; //case

if (txt[i]='0') then strlevel[i]:='';

//要處理零 以及加上萬、億

if n-i=4 then

begin

if strarray[i]='零' then strarray[i]:='';

result:=strarray[i]+strlevel[i]+'萬'+result

endelse if n-i=8 then

begin

if strarray[i]='零' then strarray[i]:='';

result:=strarray[i]+strlevel[i]+'億'+result

end //begin

else if n-i=12 then

begin

if strarray[i]='零' then strarray[i]:='';

result:=strarray[i]+strlevel[i]+'兆'+result

end //begin

else

result:=strarray[i]+strlevel[i]+result;

end; //for

end;

人民幣數字大寫轉換

中文大寫金額數字應用壹 貳 叄 肆 伍 陸 柒 捌 玖 拾 佰 仟 萬 億 元 角 分 零 整 正 等字樣。中文大寫金額數字到 元 為止的,在 元 之後,應寫 整 或 正 字,在 角 之後,可以不寫 整 或 正 字。中文大寫金額數字前應標明 人民幣 字樣,大寫金額數字有 分 的,分 後面不寫 整 或...

人民幣 Rmb 數字轉換大寫 大寫

package com.test public class rmbconvert 段內的量度 char vunit 段間的量度 char digit 小寫對應的大寫 long longmoney long money 100 string strmoney string.valueof longmo...

人民幣大寫轉換為數字

public class rmbutils 中文中繁寫的漢字金額 經常使用 public static final string big rmb numbers new string 大寫的漢字 與漢字相應的轉化的數字 public static final long to arabic numbe...