项目中使用,网上找不到,自己写了一个
type
TChnnum=record
rate:integer;
str:string;
end;
const chndigit:array[0..9]of Char=('零','一','二','三','四','五','六','七','八','九');
function TTabbedwithNavigationForm.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 begin
x:=10;
inc(i);
end;
while i<s.Length do
begin
a:=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;
本文介绍了一种将中文数字转换为整数的方法,并提供了Delphi语言的具体实现细节。通过定义记录类型TChnnum来存储数字的比率和对应的字符串表示,利用数组chndigit映射0到9的中文字符。函数ChnToNum遍历输入的中文数字字符串,将其解析为相应的整数值。
996

被折叠的 条评论
为什么被折叠?



