⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainformunit.pas

📁 <数字转换汉字大写和英文读法的几个函数> 很早以前写的一个数字转换汉字大写和英文读法的几个函数,02年还修改过,可应用于delphi和Powerbuider,有兴趣的朋友可以改成其他
💻 PAS
📖 第 1 页 / 共 4 页
字号:
    if str2='' then//如果为纯整数
     result:=NumHead+intstr+'元整'
    else
     begin
      if intstr='零' then//如果整数为零,就只显示小数
       result:=NumHead+decstr
      else
       result:=NumHead+intstr+'元'+decstr
     end;
   end;
  if cntype=1 then//将数字字串转换为普通大写格式
   begin
    if str2='' then//如果为纯整数
     result:=NumHead+intstr
    else
     result:=NumHead+intstr+'点'+decstr
   end;
  {转换完毕}
end;
{***********************数字转换汉字函数B定义***********************}
function TMainForm.DecimalToChineseExt(number:string;cntype:integer):string;
{
 介绍:参数number为string类型,数字格式
 cntype=0:转换为人民币大写格式
 cntype=1:转化为数字大写格式
 例如:DecimalToChineseExt('45092034.54',0)  :'肆仟伍佰零玖万贰仟零叁拾肆元伍角肆分
       DecimalToChineseExt('45092034.54',1)  :'肆仟伍佰零玖万贰仟零叁拾肆点伍肆
 输入数字精度无任何限制,超过两位小数的当转换为人民币格式时自动四舍五入为两位小数
}
var
  numstr:array[1..12] of string;//中文数位名
  str1,str2,intstr,decstr:string;//整数部分,小数部分,大写整数部分,大写小数部分
  num:array[1..10] of string;//各个数位段,即每四位为一段
  n:array[1..10] of string;//各位数字
  number1,number2,number3:string;//定义数字的三个数位段
  pointpos:integer;//小数点位置
  decpos:integer;
  NumHead:string;//正负性
  //NumLen:integer;//参数Number的串长度
  function DecStrRound(DecStr:String;Digit:integer;FillZero:boolean):string;//四舍五入函数
    //本函数将数字格式的字符串四舍五入
    //Digit:保留小数位数
    //FillZero:少于保留位数则用0填充
  var i,P,PotLt:integer;
      DecError:boolean;
      FrontStr,TailStr,TempStr:string;
      Carry:Shortint;
  begin
    P:=0;
    PotLt:=0;
    DecError:=false;
    if DecStr='.' then //如果以小数点为首的数字格式
      DecStr:='0'
    else begin
      If DecStr[1]='.' then DecStr:='0'+DecStr;
    end;
    //先判断数字是否合法
    for i:=1 to Length(DecStr) do begin
      if DecStr[i]='.' then begin
        P:=P+1;PotLt:=i;//记录小数点的个数和位置
      end;
      if ((DecStr[i]<'0') and (DecStr[i]<>'.')) or (DecStr[i]>'9') then begin
        DecError:=true;Break;   //判断有无非法字符
      end;
    end;
    if P>1 then DecError:=true;  //如果小数点超过1则数字非法
    if DecError then begin
      //showmessage('参数无效');
      Result:='';
      Exit;  //输入数字非法
    end;

    if P=0 then begin//如果为纯整数,直接输出
      Result:=DecStr;
      if FillZero then begin //如果需要补0,则补足小数部分的空位为0
        Result:=Result+'.';
        for i:=1 to digit do begin
          Result:=Result+'0';
        end;
      end;
      Exit;
    end;
    //如果存在小数,分别就整数段和小数段分析
    FrontStr:=Copy(DecStr,1,PotLt-1);
    TailStr:=Copy(DecStr,PotLt+1,Length(DecStr)-PotLt);
    //去掉小数部分后面的0字符
    while copy(Tailstr,Length(Tailstr),1)='0' do begin
      Tailstr:=Copy(Tailstr,1,Length(Tailstr)-1);
    end;
    //如果保留小数位数大于等于目前的小数位数
    if Digit>=Length(TailStr) then begin
      if FillZero then begin //如果需要补0,则补足小数部分的空位为0
        for i:=1 to Digit-Length(TailStr) do begin
          DecStr:=DecStr+'0';  //当保留位数大于,自动补0
        end;
      end;
      Result:=DecStr;
      Exit;
    end;
    TempStr:='';
    Carry:=0;
    if strtoint(TailStr[Digit+1])>=5 then Carry:=1;//进位
    //先从小数开始分析,四舍五入
    for i:=Digit Downto 1 do begin
      p:=strtoint(TailStr[i])+Carry;
      if p=10 then begin
        if Fillzero then  Tempstr:=Tempstr+'0';
        Carry:=1;
      end else begin
        Tempstr:=Tempstr+inttostr(p);
        if not Fillzero then begin
          if Tempstr='0' then Tempstr:='';//不想填0时,如果返回结果等于0,则清空结果
        end;
        Carry:=0;
      end;
    end;
    if Tempstr<>'' then  Tempstr:=Tempstr+'.';
    //小数部分分析完毕,开始分析整数部分
    for i:=length(FrontStr) downto 1 do begin
      p:=strtoint(FrontStr[i])+Carry;
      if p=10 then begin
        Tempstr:=Tempstr+'0';
        Carry:=1;
      end else begin
        Tempstr:=Tempstr+Inttostr(p);
        Carry:=0;
      end;
    end;
    if Carry=1 then Tempstr:=Tempstr+'1';//到最后还存在进位,则进1
    //处理所得到的结果字符串--倒置处理,得到最后的结果
    Result:='';
    for i:=Length(Tempstr) downto 1 do begin
      Result:=Result+Tempstr[i];
    end;
  end;
begin
  if (Cntype<>0) and (CnType<>1) then begin
   showmessage('类型参数错误!');
   result:='';
   exit;
  end;
  if trim(number)='' then begin
    showmessage('参数空');
    result:='';
    exit;
  end;
  {初始化}
  numstr[1] :='零';
  numstr[2] :='壹';
  numstr[3] :='贰';
  numstr[4] :='叁';
  numstr[5] :='肆';
  numstr[6] :='伍';
  numstr[7] :='陆';
  numstr[8] :='柒';
  numstr[9] :='捌';
  numstr[10]:='玖';
  numstr[11]:='拾';
  str1:='';
  str2:='';
  intstr:='';
  decstr:='';
  //若第一位有”-“号,则取出
  NumHead:='';
  if number[1]='-' then //可能是负数
  begin
   Number:=copy(Number,2,length(number)-1);
   NumHead:='负';
  end;
  //将形同'.'或者'.XXX'的数字加上整数位0
  if Number='.' then
    Number:='0'
  else begin
    if Number[1]='.' then Number:='0'+Number;
  end;
  //先校验参数number是不是数字格式
  //校验是否只有一个小数点
  Decpos:=0;
  for PointPos:=1 to Length(Number) do begin
   if Number[PointPos]='.' then Decpos:=Decpos+1;
  end;
  if Decpos>1 then begin
   showmessage('输入参数不是数字格式!');
   Result:='';
   exit;
  end;
  //校验是否含有非数字和'.'的字母存在
  for pointPos:=1 to Length(Number) do begin
   if Number[pointpos]='.' then continue;
   if  (ord(Number[pointpos])<48) or (ord(Number[pointpos])>57) then begin
    Result:='error';
    break;
   end;
  end;
  if Result='error' then begin
    showmessage('输入参数不是数字格式!');
    result:='';
    exit;
  end;

  //如果转换为人民币大写,对输入数字四舍五入处理,保留2位小数
  if Cntype=0 then  Number:=DecStrRound(Number,2,false);
  pointpos:=pos('.',Number);//得到小数点的位置

  //以下将得到整数部分Str1和小数部分Str2
  if pointpos=0 then//输入值为纯整数
   begin
     str1:=Number;
     str2:=''
   end
  else//输入值为浮点数
   begin
     str1:=copy(Number,1,PointPos-1);//取整数部分
     str2:=copy(Number,PointPos+1,Length(Number)-PointPos);//取小数部分
     //消除小数后面多余的0
     while copy(str2,length(str2),1)='0' do str2:=copy(str2,1,length(str2)-1);
   end;
   //消除整数部分前面的多余的0
   while copy(str1,1,1)='0' do str1:=copy(str1,2,length(str1)-1);
   if str1='' then str1:='0';
  {*****分析转换整数部分*****}
  {分析整数部分在100000000以上的}
  if length(str1)>8 then
   begin
     //按每4位为一段拆分成三段,逐段分析
     num[1]:=copy(str1,1,length(str1)-8);//取8位以上的那部分数段
     num[2]:=copy(str1,length(str1)-7,4);//取千万到万的4位
     num[3]:=copy(str1,length(str1)-3,4);//取千到个位的4位
     number1:=DecimalToChineseExt(num[1],1)+'亿';//通过函数嵌套调用,得到亿上的数段格式,即若干亿

     if strtoint(Num[2])=0 then//如果千万到万4位为0
     begin
       if strtoint(Num[3])=0 then//并且末尾4位为0
        Number2:=''//没有内容
       else
       begin
        if strtoint(Num[3])<1000 then
         Number2:=''//如果第三段也是零XX百十个,则中间段的"零"去掉
        else
         Number2:=numstr[1];//读零
       end;
     end
     else
     begin
       if strtoint(num[2])>1000 then //中间4位大于1000
        Number2:=DecimalToChineseExt(num[2],1)+'万'
       else
        Number2:=numStr[1]+DecimalToChineseExt(num[2],1)+'万';//不足一千万,则读X亿零xx百、十、万
     end;
     if strtoint(Num[3])=0 then//末尾4位为0
      Number3:=''
     else
     begin
       if strtoint(num[3])>1000 then//末尾4位大于1000
        Number3:=DecimalToChineseExt(num[3],1)
       else
        Number3:=numstr[1]+DecimalToChineseExt(num[3],1);//不足一千,则读X万零XX百、十
     end;
     intstr:=number1+number2+number3;
   end;

   {分析整数部分在10000~99999999之间的}
  if (length(str1)>=5) and (length(str1)<=8) then
    begin
     num[1]:=copy(str1,1,length(str1)-4);//取得第一段(千万位到万位)
     //为方便分析,若不足4位,用'0'补齐为4位
     if length(num[1])=3 then
      num[1]:='0'+num[1];
     if length(num[1])=2 then
      num[1]:='00'+num[1];
     if length(num[1])=1 then
      num[1]:='000'+num[1];
     num[2]:=copy(str1,length(str1)-3,4);//取得第二段(千位到个位)
     number1:=DecimalToChineseExt(num[1],1)+'万';
     if strtoint(num[2])=0 then
      number2:=''
     else
      begin
       if strtoint(num[2])>1000 then //中间4位大于1000
        Number2:=DecimalToChineseExt(num[2],1)
       else
        Number2:=numStr[1]+DecimalToChineseExt(num[2],1);
      end;
     intstr:=number1+number2;
   end;
  {分析整数部分不到10000的}
  if length(str1)<5 then
    begin
     num[1]:=str1;
     //不足4位,用'0'补齐
     if length(num[1])=3 then
      num[1]:='0'+num[1];
     if length(num[1])=2 then
      num[1]:='00'+num[1];
     if length(num[1])=1 then
      num[1]:='000'+num[1];
     number1:='';//亿以上的为空
     number2:='';//万以上的为空
     //分析千位
     if copy(num[1],1,1)='0' then
      n[1]:=''
     else
      n[1]:=numstr[strtoint(copy(num[1],1,1))+1]+'仟';
     //分析百位
     if copy(num[1],2,1)='0' then
      begin
       if copy(num[1],1,1)='0' then
        n[2]:=''
       else
        n[2]:=numstr[1]
      end
     else
      n[2]:=numstr[strtoint(copy(num[1],2,1))+1]+'佰';

     //分析十位
     if copy(num[1],3,1)='0' then

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -