关于数字转换为大写数字的问题。(紧急求助)(80分)

王芳

Unregistered / Unconfirmed
GUEST, unregistred user!
为了报表的需要,我必须在打印时,把从存储过程中读出的数字金额一一对应
转换为大写数字金额。请问大虾如何在存储过程中就把这一组数据转换成功?
请详细告知。
 
请您在“欢迎页”中检索一下关键词“大写”,已经有人回答了这个问题。
 
并请留意我在那个问题最后的评论,因为那个题目是我结束的,
因此给出了各个方法的评测结果。
 
看了yysun、dwwang的建议,我准备进去转一圈,无柰一直不能完整看到答案,所以
dwwang的结束语看不到,但我想应该满精彩的。我想以后再光顾。所以我准备给yysun
60分,给dwwang20分.
 
这么快就给分了! 还有机会吗?我给你寄一份去!
 
小草不用着急嘛!^_^ 那些答案中就包括你写的嘛!
但是有一点点问题,和钱达智先生的结合一下就好了。
 
下例转换结果: 4002.02->肆仟元零贰分
40100.00->肆万零一佰元正
procedure ReplaceAll(var tag: string;
str1,str2: string);
var
i: integer;
begin
while pos(str1,tag)<>0do
begin
i:=pos(str1,tag);
delete(tag,i,length(str1));
insert(str1,tag,i);
end;
end;

function change2captal(x: real):string;
const
digits: array [0..9] of string = ('零','壹','贰','叁','肆',
'伍','陆','柒','捌','玖');
positions: array [1..11] of string = ('亿','仟','佰','拾','万',
'仟','佰','拾','元','角','分');
var
s: string;
i,j: integer;
begin
s:=format('%9.2f',[x]);
result:='';
for i:=1 to length(s)do
begin
if s=' ' then
continue;
result:=result+digits[ord(s)-48]+positions;
end;
ReplaceAll(result, '零仟', '零');
ReplaceAll(result,'零佰','零');
ReplaceAll(result,'零十','零');
ReplaceAll(result,'零角','零');
ReplaceAll(result,'零分','');
while pos('零零',result)<>0do
delete(result,pos('零零',result),2);
ReplaceAll(result,'零亿','亿');
ReplaceAll(reuslt,'零万','万');
ReplaceAll(result,'零元','元');
ReplaceAll(result,'亿万','亿');
if copy(result,length(result)-1,2)='零' then
result:=copy(result,1,length(result)-2);
if copy(result,length(result)-1,2)<>'分' then
result:=result+'正';
end;
 
勘误:
1. s:=format('%11.2f',[x]);
2. 忘记判断'.'了, 更改如下
j:=0;
^^^^^
for i:=1 to length(s)do

begin

if s=' ' then

continue;
//---------------------- 添加上
if s='.' then

begin
j:=1;
continue;
end
//----------------------
result:=result+digits[ord(s)-48]+positions[i - j];

end;
......
在最后
if copy(result,length(result)-1,2)<>'分' then

result:=result+'正';
之前加
if copy(result,1,2)='元' then
result:=copy(result,3,length(result));
最后的判断改成
if result='' then

result:='零'
else
if copy(result,length(result)-1,2)<>'分' then
result:=result+'正';
这样 0.42可以得到'肆角贰分'
0.00->零
最大可转换数 999999999.99 即十亿 - 0.01
不能转换负数.
如果要转换更大的数, 只要在positions的定义里加'仟佰拾'等, 然后format里的位
数扩大即可
 
感谢dwwang指点!最近两天比较忙,昨天打印回家看了看,今天上班将那个错改
了,请大家再看看还有什么问题! ^_^
function FourNumToChnNum(Str:string;ChnNum:string;var Pre:boolean):string;
const
ChnNum2='零壹贰叁肆伍陆柒捌玖';
var
i,j,Len:integer;
begin
Result := '';
Len := Length(str) ;
for i:=1 to Lendo
begin
j := Ord(str)-48;
if j=0 then
Pre := True
else
begin
if Pre then
Result := Result + '零';
Result := Result + Copy(ChnNum2,j*2+1,2) + Trim(Copy(ChnNum,(Len - i) * 2+1,2));
Pre := False;
end;
end;
end;

function StringToChnNum(str:string):string;
const
ChnNum1='圆万亿兆';
var
i,Len,Len1,Level,Start:integer;
s1,s:string;
Pre: Boolean;
begin
Result := '';
Len := Pos('.',str)-1;
Level := (Len + 3) div 4 ;
Len1 := Len mod 4 ;
if Len1=0 then
Len1 := 4;
Start := 1;
Pre := False;
for i := 1 to Leveldo
begin
s := Copy(str,Start,Len1);
s1 := FourNumToChnNum(s,' 拾佰仟',Pre);
// 注意有两个空格
if s1&amp;lt&amp;gt'' then
Result := Result + s1 + Copy(ChnNum1,(Level-i)*2+1,2);;
Start := Start + Len1;
Len1 := 4;
end;
s1 := FourNumToChnNum(Copy(str,Len+2,2),'分角',Pre);
if s1 = '' then
s1 := '整';
Result := Result + s1 ;
end;

function RealToChnNum(realnum:real;Width:integer):string;
var
s:string;
begin
Str(realnum:Width:2,s);
Result := StringToChnNum(Trim(s));
end;
 
^_^,这儿这么热闹!
我好感谢大家的支持,可怎么分这分数呢?:(
僧多粥少......这样吧,我给最早提供信息的yysun 30分,给littlegrass 20分,
给Another eYes 20分,给dwwang 10分
 
我会受之有愧的 ^_*
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
950
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
774
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
顶部