这是我写的一个大小写函数,直接调用 function My_StrToRMB(curs :string) :string; implementation
function My_StrToRMB(curs: string) :string ; var daxie,danwei,minuscurs:string; i,j,deccount :integer ; rmb :int64;
begin curs:=trim(curs); if (curs='-') or (curs='.') or (curs=') then // '.','-','错 begin result:='ERROR'; exit; end; deccount :=0; for i:=1 to length(curs) do begin if not (curs[i] in ['0'..'9','.','-']) then //'123w2'错 begin result:='ERROR'; exit; end; if (curs[i]='.') and (deccount>0) then //'12313.324.23'错 begin result:='ERROR'; exit; end; if (curs[i]='-') and (i>1) then //'-123-123'错 begin result:='ERROR'; exit; end; if curs[i]='.' then inc(deccount); end; rmb:=round(StrToFloat(curs)*100); minuscurs:='; //负数标志 if rmb<0 then begin minuscurs:='(负数)' ; rmb:=(-1)*rmb; end; if rmb>=1E18 then //超过9千万亿 begin result:='ERROR'; exit; end; curs:='; i:=0; j:=0 ; while rmb>0 do begin j:= rmb mod 10; case j of 0 : daxie :='零' ; 1 : daxie :='壹' ; 2 : daxie :='贰' ; 3 : daxie :='叁' ; 4 : daxie :='肆' ; 5 : daxie :='伍' ; 6 : daxie :='陆' ; 7 : daxie :='柒' ; 8 : daxie :='捌' ; 9 : daxie :='玖' ; end; case i of 0 : danwei :='分' ; 1 : danwei :='角' ; 2 : danwei :='圆' ; 3 : danwei :='拾' ; 4 : danwei :='佰' ; 5 : danwei :='仟' ; 6 : danwei :='万' ; 7 : danwei :='拾' ; 8 : danwei :='佰' ; 9 : danwei :='仟' ; 10 : danwei :='亿' ; 11 : danwei :='拾' ; 12 : danwei :='佰' ; 13 : danwei :='仟' ; 14 : danwei :='万' ; 15 : danwei :='拾' ; 16 : danwei :='佰' ; 17 : danwei :='仟' ; end; rmb:=rmb div 10; if j<>0 then curs:=daxie+danwei+curs; //该位上不为0 if (j=0) and (not (i in [2,6,10,14])) then //该位为0,是一般位 curs:=daxie+curs; if (j=0) and (i in [2,6,10,14]) then //该位为0,是敏感位 curs:=danwei+curs; inc(i); end; while pos('零零',curs)>0 do curs:=stringreplace(curs,'零零','零',[]); curs:=stringreplace(curs,'零圆','圆',[]); while pos('零万',curs)>0 do curs:=stringreplace(curs,'零万','万',[]); //上万亿后可能两个'零万' curs:=stringreplace(curs,'零亿','亿',[]); curs:=stringreplace(curs,'角零','角整',[]); if copy(curs,length(curs)-3,4)='圆零' then //最后两位是圆零. curs:=stringreplace(curs,'圆零','圆整',[]); //小数点后 curs:=stringreplace(curs,'亿万','亿',[]); result:=minuscurs+curs; end;
//我曾用DBaseIII编写过类似函数,虽仅十几行代码却完全符合财务规定。至今未发现有更为短小精简的。现改编为Delphi程序,请各位指正。 //以下函数能将小于十万亿元的小写金额转换为大写 //作者:方小庆 inrm@263.net Function NtoC(n0 :real) :String; Function IIF(b :boolean; s1,s2:string):string; begin {本函数在VFP和VB均为系统内部函数} if b then IIF:=s1 else IIF:=s2; end; Const c = '零壹贰叁肆伍陆柒捌玖◇分角圆拾佰仟万拾佰仟亿拾佰仟万'; var L,i,n, code :integer; Z :boolean; s, st,st1 :string; begin s :=FormatFloat('0.00',n0); L :=Length(s); Z :=n0<1; For i:= 1 To L-3 do begin Val(Copy(s, L-i-2, 1), n, code); st:=IIf((n=0)And(Z Or(i=9)Or(i=5)Or(i=1)), ', Copy(c, n*2+1, 2)) + IIf((n=0)And((i<>9)And(i<>5)And(i<>1)Or Z And(i=1)), ', Copy(c, (i+13)*2-1, 2)) + st; Z := (n=0); end; Z := False; For i:= 1 To 2 do begin Val(Copy(s, L-i+1, 1), n, code); st1:=IIf((n=0)And((i=1)Or(i=2)And(Z Or (n0<1))),',Copy(c,n*2+1,2)) + IIf((n>0), Copy(c,(i+11)*2-1, 2), IIf((i=2) Or Z, ', '整')) + st1; Z := (n=0); end; For i :=1 To Length(st) do If Copy(st, i, 4) = '亿万' Then Delete(st,i+2,2); NtoC := IIf(n0 = 0, '零', st+st1); End;
function NumTOChinaCode(NN:real):string; var HZ,NS,NW,NA,N1,N2:string; LA,X,Nk:integer; begin if NN>9999999999999.99 then begin HZ:='; Result:=HZ; exit; end; if NN=0 then begin HZ:='零元'; result:=HZ; exit; end; NS:='零壹贰叁肆伍陆柒捌玖'; NW:='分角元拾佰仟万拾佰仟亿拾佰仟万'; NA:=FloatToStr(NN*100); LA:=length(NA); X:=1; HZ:='; while X<=LA do begin NK:=Ord(NA[x])-Ord('0'); N1:=Copy(NS,NK*2+1,2); N2:=Copy(NW,LA*2+1-X*2,2); if (NK=0) AND ((N2='亿') OR( N2='万') OR( N2='元'))then begin if copy(HZ,Length(HZ)-1,2)='零' then HZ:=copy(HZ,1,length(HZ)-2); if copy(HZ,Length(HZ)-1,2)='亿' then if N2='元' then begin N1:=N2; N2:='零'; end else N2:=' else begin N1:=N2; N2:='零'; end end else if NK=0 then begin if copy(HZ,length(HZ)-1,2)='零' then N1:='; if N2='分' then begin if copy(HZ,length(HZ)-1,2)='零' then HZ:=copy(HZ,1,length(HZ)-2)+'整' else HZ:=HZ+'整'; N1:='; end; N2:='; end; HZ:=HZ+N1+N2; X:=X+1 end; Result:=HZ; end;
----------------------------------------------
-
function TForm1.Convert(money:real):string; var smallMode:string; bigChar,powerChar:string[2]; power,dotPos,i:integer; begin power:=-2; smallMode:=formatfloat('0.00',money); dotPos:=System.Pos('.',smallMode); for i:=length(smallMode) downto 1 do begin if i=doPos then continue; case strtoint(copy(smallMode,i,1)) of 1:bigchar:='壹'; 2: ...... 9:bigchar:='酒'; 0:bigchar:='零'; end; case power of -3:powerchar:='厘'; -2:powerchar:='分'; -1:powerchar:='角'; 0:powerchar:='元'; 1,5,9:powerchar:='拾'; 2,6,10:powerchar:='佰'; 3,7,11:powerchar:='千'; 4,12:powerchar:='万'; 8:powerchar:='亿'; end; inc(power); Result:=bigchar+powerchar+Result; end; end; 调用时:convert(strtofloat(//string)); 请大家试用,
----------------------------------------------
我爱delphi
我原以为自己写的函数已不能再简短了,算法也已是最合理的。但是看了 11楼 ljeana 朋友推荐的代码后,深感自己的浅薄。借用其思路并将自己的代码改进如下,抛砖引玉,请各位指正: //以下函数能将小于十万亿元的小写金额转换为大写 //作者:方小庆 inrm@263.net Function NtoC(n0 :real) :String; Function IIF(b :boolean; s1,s2:string):string; begin {本函数在VFP和VB均为系统内部函数} if b then IIF:=s1 else IIF:=s2; end; Const c = '零壹贰叁肆伍陆柒捌玖◇分角圆拾佰仟万拾佰仟亿拾佰仟万'; var L,i,n, code :integer; Z :boolean; s, st,st1 :string; begin s:= FormatFloat('0',n0*100); L:= Length(s); For i:=1 to L do begin n:= StrToInt( Copy(s, L-i+1, 1)); P:= (i=11)or(i=7)or(i=3)or(i=1); //亿、万、元、分位 st:=IIF((n=0)and(Z or P),', Copy(c,n*2+1,2)) //数值 + IIF((n=0)and(i=1),'整', //分位为零 IIF((n>0)or P, Copy(c,(i+11)*2-1,2),')) //单位 + IIF((n=0)and(not Z)and(i>1)and P,'零',') //亿、万、元位为零而千万、千、角位不为零 + st; Z:= n=0; end; For i:=1 To Length(st) do If Copy(st,i,4)='亿万' Then Delete(st,i+2,2); //亿位和万位之间都是零时会出现'亿万' result:= IIF(n0>9999999999999.99, '溢出', IIf(n0 = 0, '零', st)); End; //谢谢了 11楼的 ljeana 朋友!
//整数部分处理 ls_capital_num = "" If Integer(ls_integer) <> 0 Then lb_begin = True li_last_num = 10 For li_i = 0 To li_len - 1 li_num = Integer(Mid(ls_integer, li_len - li_i, 1)) li_y = Truncate(li_i / 4, 0) li_x = li_i - li_y * 4 If li_num <> 0 Then ls_capital[li_x, li_y] = ls_num[li_num] + ls_unit[li_x, li_y] lb_begin = False Else If li_x = 0 Then If li_y <> 0 And li_last_num <> 0 Then ls_capital[li_x, li_y] = ls_unit[li_x, li_y] + '零' Else ls_capital[li_x, li_y] = ls_unit[li_x, li_y] End If Else If Not lb_begin And li_last_num <> 0 Then ls_capital[li_x, li_y] = '零' Else ls_capital[li_x, li_y] = ' End If End If //当本段四位数字全为 0 时,不需要本段四位数字的单位 If li_x = 3 Then If Mid(ls_integer, li_len - (li_y * 4 + li_x), 4) = '0000' And li_y <> 0 Then ls_capital[0, li_y] = Mid(ls_capital[0, li_y], 3) End If End If End If li_last_num = li_num Next For li_i = 0 To li_len - 1 li_y = Truncate(li_i / 4, 0) li_x = li_i - li_y * 4 ls_capital_num = ls_capital[li_x, li_y] + ls_capital_num Next End If
//小数部分处理,即添加角分 If Integer(ls_decimal) <> 0 Then li_jiao = Integer(Left(ls_decimal, 1)) li_fen = Integer(Right(ls_decimal, 1)) If li_jiao > 0 Then ls_capital_num = ls_capital_num + ls_num[li_jiao] + "角" End If If li_fen > 0 Then If li_jiao = 0 And Long(ls_integer) > 0 Then ls_capital_num = ls_capital_num + '零' End If ls_capital_num = ls_capital_num + ls_num[li_fen] + "分" End If End If
//添加“整” If Integer(ls_decimal) = 0 Then ls_capital_num = ls_capital_num + "整" ElseIf Integer(ls_decimal) <> 0 Then li_fen = Integer(Right(ls_decimal, 1)) If li_fen = 0 Then ls_capital_num = ls_capital_num + "整" End If
//添加“负” If ld_num < 0 Then ls_capital_num = "负" + ls_capital_num End If
if (ld_num > -0.0000001) and (ld_num < 0) then ls_capital_num = '零元整' end if