各位大虾,请指点(0分)

L

L.Q

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是小弟写的数字转换成中文字符的程序,好像代码太多,请多位指点指点。

Function TForm1.IntChangChar(var IntStr:String):String;
var
NewStr,Str_dw:String;
iCount:integer;
Str_pj:Char;
begin
NewStr:='';

for iCount:=1 to length(IntStr) do
begin
Str_pj := IntStr[iCount];
case Str_pj of //把小写字符转换成大写字符
'0': Str_dw := '零';
'1': Str_dw := '壹';
'2': Str_dw := '贰';
'3': Str_dw := '叁';
'4': Str_dw := '肆';
'5': Str_dw := '伍';
'6': Str_dw := '陆';
'7': Str_dw := '柒';
'8': Str_dw := '捌';
'9': Str_dw := '玖';
'.': Str_dw := '点';
end;

case Length(IntStr) of
12:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 亿 ' ;
2: newstr:=newstr+Str_dw+' 千 ' ;
3: newstr:=newstr+Str_dw+' 百 ' ;
4: newstr:=newstr+Str_dw+' 十 ' ;
5: newstr:=newstr+Str_dw+' 万 ' ;
6: newstr:=newstr+Str_dw+' 千 ' ;
7: newstr:=newstr+Str_dw+' 百 ' ;
8: newstr:=newstr+Str_dw+' 十 ' ;
9: newstr:=newstr+Str_dw+' 圆 ' ;
10: newstr:=newstr+Str_dw+' ' ;
11: newstr:=newstr+Str_dw+' 角 ' ;
12: newstr:=newstr+Str_dw+' 分 ' ;
end;

11:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 千 ' ;
2: newstr:=newstr+Str_dw+' 百 ' ;
3: newstr:=newstr+Str_dw+' 十 ' ;
4: newstr:=newstr+Str_dw+' 万 ' ;
5: newstr:=newstr+Str_dw+' 千 ' ;
6: newstr:=newstr+Str_dw+' 百 ' ;
7: newstr:=newstr+Str_dw+' 十 ' ;
8: newstr:=newstr+Str_dw+' 圆 ' ;
9: newstr:=newstr+Str_dw+' ' ;
10: newstr:=newstr+Str_dw+' 角 ' ;
11: newstr:=newstr+Str_dw+' 分 ' ;
end;

10:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 百 ' ;
2: newstr:=newstr+Str_dw+' 十 ' ;
3: newstr:=newstr+Str_dw+' 万 ' ;
4: newstr:=newstr+Str_dw+' 千 ' ;
5: newstr:=newstr+Str_dw+' 百 ' ;
6: newstr:=newstr+Str_dw+' 十 ' ;
7: newstr:=newstr+Str_dw+' 圆 ' ;
8: newstr:=newstr+Str_dw+' ' ;
9: newstr:=newstr+Str_dw+' 角 ' ;
10: newstr:=newstr+Str_dw+' 分 ' ;
end;

9:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 十 ' ;
2: newstr:=newstr+Str_dw+' 万 ' ;
3: newstr:=newstr+Str_dw+' 千 ' ;
4: newstr:=newstr+Str_dw+' 百 ' ;
5: newstr:=newstr+Str_dw+' 十 ' ;
6: newstr:=newstr+Str_dw+' 圆 ' ;
7: newstr:=newstr+Str_dw+' ' ;
8: newstr:=newstr+Str_dw+' 角 ' ;
9: newstr:=newstr+Str_dw+' 分 ' ;
end;

8:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 万 ' ;
2: newstr:=newstr+Str_dw+' 千 ' ;
3: newstr:=newstr+Str_dw+' 百 ' ;
4: newstr:=newstr+Str_dw+' 十 ' ;
5: newstr:=newstr+Str_dw+' 圆 ' ;
6: newstr:=newstr+Str_dw+' ' ;
7: newstr:=newstr+Str_dw+' 角 ' ;
8: newstr:=newstr+Str_dw+' 分 ' ;
end;

7:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 千 ' ;
2: newstr:=newstr+Str_dw+' 百 ' ;
3: newstr:=newstr+Str_dw+' 十 ' ;
4: newstr:=newstr+Str_dw+' 圆 ' ;
5: newstr:=newstr+Str_dw+' ' ;
6: newstr:=newstr+Str_dw+' 角 ' ;
7: newstr:=newstr+Str_dw+' 分 ' ;
end;

6:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 百 ' ;
2: newstr:=newstr+Str_dw+' 十 ' ;
3: newstr:=newstr+Str_dw+' 圆 ' ;
4: newstr:=newstr+Str_dw+' ' ;
5: newstr:=newstr+Str_dw+' 角 ' ;
6: newstr:=newstr+Str_dw+' 分 ' ;
end;

5:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 十 ' ;
2: newstr:=newstr+Str_dw+' 圆 ' ;
3: newstr:=newstr+Str_dw+' ' ;
4: newstr:=newstr+Str_dw+' 角 ' ;
5: newstr:=newstr+Str_dw+' 分 ' ;
end;

4:
case iCount of //换成大写
1: newstr:=newstr+Str_dw+' 圆 ' ;
2: newstr:=newstr+Str_dw+' ' ;
3: newstr:=newstr+Str_dw+' 角 ' ;
4: newstr:=newstr+Str_dw+' 分 ' ;
end;

end;

end;
Result := '¥'+NewStr;
end;
 
给你一个:
{***********************数字转换汉字函数A定义***********************}
function TTestForm.DecimalToChinese(number:Extended;cntype:integer):string;
{介绍:参数number为Extended类型
cntype=0:转换为人民币大写格式
cntype=1:转化为数字大写格式
例如:decimaltocn(45092034.541,0) :'肆仟伍佰零玖万贰仟零叁拾肆元伍角肆分
decimaltocn(45092034.541,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;//正负性
NumberName:string;//用于代替参数Number的字符串
NumLen:integer;//参数Number的串长度
begin
if (Cntype<>0) and (CnType<>1) 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:='';
if Number<0 then//如果是负数,则在前面加‘负'
begin
NumHead:='负';
Number:=-Number;
end
else
NumHead:='';
NumberName:=Floattostr(Number);
NumLen:=Length(NumberName);
pointpos:=pos('.',NumberName);
//以下将得到整数部分Str1和小数部分Str2
if pointpos=0 then//输入值为纯整数
begin
str1:=NumberName;
str2:=''
end
else//输入值为浮点数
begin
if cntype=0 then//如果转换为人民币大写,并且小数数位超过2位,则只保留2位
if (NumLen-pos('.',numberName))>2 then NumberName:=trim(format('%'+inttostr(NumLen)+'.2f',[Number]));
str1:=copy(numberName,1,pos('.',numberName)-1);//取整数部分
str2:=copy(numberName,pos('.',numberName)+1,length(numberName)-pos('.',numberName));//取小数部分
if strtoint64(str2)=0 then //如果小数部分为0,则取消小数部分的分析
str2:='';
end;
{*****分析转换整数部分*****}
{分析整数部分在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:=DecimalToChinese(strtoint64(num[1]),1)+'亿';//通过函数嵌套调用,得到亿上的数段格式,即若干亿

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

//分析十位
if copy(num[1],3,1)='0' then
begin
if copy(num[1],2,1)='0' then
n[3]:=''
else
n[3]:=numstr[1]
end
else
begin
if (copy(num[1],1,1)='0') and (copy(num[1],2,1)='0') and (copy(num[1],3,1)='1') then//如果百位为0且十位为1则不读出壹字
n[3]:='拾'
else
n[3]:=numstr[strtoint64(copy(num[1],3,1))+1] +'拾';
end;

//分析个位
if copy(num[1],4,1)='0' then
n[4]:=''
else
n[4]:=numstr[strtoint64(copy(num[1],4,1))+1];

//
if copy(num[1],length(num[1])-2,3)='000' then//当末尾有000时
begin
n[2]:='';
n[3]:='';
n[4]:=''
end;
if copy(num[1],length(num[1])-1,2)='00' then//当末尾有00时
begin
n[3]:='';
n[4]:=''
end;
if copy(num[1],length(num[1]),1)='0' then//当末尾有0时
n[4]:='';
//数段合并
number3:=n[1]+n[2]+n[3]+n[4];
//取得整数位值
intstr:=number1+number2+number3;
end;
{如果整数为零,转换为"零"}
if str1='0' then
intstr:=numstr[1];

{整数转换完毕}


{*****分析和转换小数部分*****}
if length(str2)>0 then //如果小数数段不为空,则分析小数
begin
if cntype=0 then{一.如果转换为人民币表达式}
begin
//不足2位,用零补足空位
if length(str2)=1 then str2:=str2+'0';
if copy(str2,1,1)='0' then//角为0
begin
if Intstr='零' then n[1]:='' else n[1]:='零';//如果元为0,则不读0角,否则读零若干分
end
else
n[1]:=numstr[strtoint64(copy(str2,1,1))+1]+'角';
if copy(str2,2,1)='0' then
n[2]:=''
else
n[2]:=numstr[strtoint64(copy(str2,2,1))+1]+'分';
decstr:=n[1]+n[2];
end
else //二.如果转换为数字表达式
begin
decstr:='';
for decpos:=1 to length(str2) do
begin
decstr:=decstr+numstr[strtoint64(copy(str2,decpos,1))+1];
end;
end;
end;
{小数转换完毕}
{输出本函数的结果***********************}
if cntype=0 then//将数字字串转换为人民币的大写格式
begin
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;
 
接受答案了.
 

Similar threads

D
回复
0
查看
896
DelphiTeacher的专栏
D
D
回复
0
查看
868
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
733
DelphiTeacher的专栏
D
D
回复
0
查看
532
DelphiTeacher的专栏
D
顶部