delphi下将中文数字转换为阿拉伯数字

来源:互联网 发布:域名目录反向解析ip 编辑:程序博客网 时间:2024/05/16 05:39
因为项目中要用到中文转阿拉伯数字,网上找不到别人的,于是自己写了一个。
type  TChnnum=record    rate:integer;    str:string;  end;  const chndigit:array[0..9]of Char=('零','一','二','三','四','五','六','七','八','九');function ChnToNum (s:string):Integer;var chnnums:array[0..3] of TChnnum ;i,j:integer;a,b,x:integer;str:string ;begin chnnums[0].rate:=10;chnnums[0].str:='十'; chnnums[1].rate:=100;chnnums[1].str:='百'; chnnums[2].rate:=1000;chnnums[2].str:='千'; chnnums[3].rate:=10000;chnnums[3].str:='万';//从左开始对字str:=s;x:=0;i:=0;if str[1] ='十' then beginx:=10;inc(i);end;while i<s.Length dobegina:=0;b:=1;inc(i);  for j:=Low(chndigit) to High(chndigit) do  if str[i] =chndigit[j] then begin     if j=0 then inc(i)     else begin      a:=j;      break;     end;  end  ;  if (a=0 ) and (i=1) then a:=1;  if i< s.Length then begin  inc(i);    for j:= Low(chnnums) to High(chnnums) do      if str[i]= chnnums[j].str  then begin       b:= chnnums[j].rate;       break;      end;   end;  x:=x+a*b;end;  Result:=x;end;

0 0
原创粉丝点击