数字转换大写人民币的delphi实现

本文介绍了一个将人民币金额从数字形式转换成汉字形式的Delphi函数。该函数首先验证输入的有效性,然后通过逐位处理来完成转换。文章还考虑了各种特殊情况,如小数点后的位数限制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<length(txt))do
    begin
      if (p^>'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;
        end
       else
       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;
    end
   else
    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 i<ilen then   //如果低位也为零,低位零不显示
          if (strarray[i+1]= '') or (strarray[i+1]= '零') then
            begin
              //strarray[i+1]:= '';
              strarray[i]:= '';
            end;
         if i=n then strarray[i]:='';
         strlevel[i]:='';
       end;
     end; //case
  end;
//先处理 小数点部分

  if m>0 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
     end
     else 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;

  

转载于:https://www.cnblogs.com/win32pro/p/7184050.html

//ChangeRMB.java /** * * programmed by HuangHeliang * 2009.04.15 10:20:51 * */ //package com.avtech.hhl; import java.io.*; public final class ChangeRMB { //每个数字对应的大写 private static final String[] num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", }; //从低到高排列的单位 private static final String[] bit = { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; //金额里面的角和分 private static final String[] jf={ "角","分" }; /** * 处理金额的整数部分,返回"...圆整" * @param integer * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer)throws Exception{ StringBuilder sbdr=new StringBuilder(""); int j=integer.length(); if(j>bit.length){ throw new Exception("\n只能处理亿万亿以内的数据(含亿万亿)!"); } char[] rmb=integer.toCharArray(); for (int i = 0; i 壹佰亿陆仟伍佰万肆仟伍佰捌拾叁圆零伍分 */ if(bit[bitLocate].equals("仟")){ String s=sbdr.toString(); if(!s.endsWith(bit[bitLocate+1]) && s.length()>0){ if (s.endsWith(num[0])) { sbdr.deleteCharAt(sbdr.length() - 1); } sbdr.append(bit[bitLocate+1]); } } sbdr.append(num[numLocate]); sbdr.append(bit[bitLocate]); }//end for /* * 去掉结尾"零"后,补全 */ if(sbdr.toString().endsWith(num[0])){ sbdr.deleteCharAt(sbdr.length()-1); sbdr.append("圆整"); }else{ sbdr.append("整"); } return sbdr.toString(); } /** * 处理带小数的金额,整数部分交由上一个方法处理,小数部分自己处理 * @param integer * @param decimal * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer, String decimal)throws Exception{ String ret=ChangeRMB.praseUpcaseRMB(integer); ret=ret.split("整")[0]; //处理整数部分 StringBuilder sbdr=new StringBuilder(""); sbdr.append(ret); char[] rmbjf=decimal.toCharArray(); for(int i=0;i rmbDouble){ theInt-=1; } double theDecimal=rmbDouble-theInt; String integer=new Long((long)theInt).toString(); String decimal=""+Math.round(theDecimal*100); if(decimal.equals("0")){ result=ChangeRMB.praseUpcaseRMB(integer); }else{ result=ChangeRMB.praseUpcaseRMB(integer, decimal); } return result; } public static void main(String[] args) throws Exception{ System.out.print("输入小写人民币金额:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String in = br.readLine(); String result=ChangeRMB.doChangeRMB(in); System.out.println("\n"+"------------转换结果------------"); System.out.println(result); double d=54628569856.68; String ret=ChangeRMB.doChangeRMB(d); System.out.println("\n"+"------------转换结果------------"); System.out.println(ret); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值