如何将小写金额转换为大写?

first_lover 2001-09-10 05:14:32
在做报表的时候,统计出一个账单的付费金额(小写),格式为¥,0.00;-¥,0.00(如:¥10,406.05),我写的代码如下:
function TForm1.GetPositionName(value: integer): string;
begin
case value of
-2: result:='分';
-1: result:='角';
//0: result:='圆';
0: result:='';
1: result:='拾';
2: result:='佰';
3: result:='仟';
4: result:='万';
5: result:='十';
6: result:='百';
7: result:='千';
8: result:='亿';
9: result:='拾';
end;
end;

function TForm1.GetBigValue(value: char): string;
begin
case value of
'0': result:='零';
'1': result:='壹';
'2': result:='贰';
'3': result:='叁';
'4': result:='肆';
'5': result:='伍';
'6': result:='陆';
'7': result:='柒';
'8': result:='捌';
'9': result:='玖';
end;
end;

function TForm1.LowertoUpper(LowerStr : Extended) : string;
var
Money: string;
i : integer;
position : integer;
HasLower : Boolean;
addZero : integer;
strMoney : string;
begin
Money := FormatFloat('0.00',LowerStr);
position := -2;
strmoney := '';
addZero := 0;
HasLower := False;
for i:=Length(Money) downto 1 do
begin
case position of
4,8:begin
if HasLower and (AddZero>0) then
begin
strMoney := GetBigValue('0') + strMoney;
HasLower := False;
AddZero := 0;
end;
end;
end;
if Money[i]<>'0' then
begin
if Money[i]<>'.' then
begin
if (addZero>0) and HasLower then
begin
strMoney := GetBigValue('0') + strMoney;
addZero := 0;
end;
case position of
-2,-1,4,8: HasLower := False;
else
HasLower := True;
end;
strMoney := GetBigValue(Money[i]) + GetPositionName(position) + strMoney;
end
else begin
position := position - 1;
strMoney := '圆'+strMoney;
end;
end
else if position >0 then
begin
addZero := addZero + 1;
case position of
0,4,8: begin
strMoney := GetPositionName(position) + strMoney;
end;
end;
end;
position := position + 1;
end;
Result := strMoney;
end;
可是运行后某些特殊的数据不能正常现实,小弟请教各位大哥给出正确的算法,不胜感激!
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hupeh 2001-12-24
  • 打赏
  • 举报
回复
function RMB(NN:real):string;
var
HZ,NS,NW,NA,N1,N2:string;
LA,X,Nk:integer;
begin
if NN>9999999999999.99 then
begin
MessageDlg('金额溢出.',mtError,[mbOk], 0);
HZ:='';
Result:=HZ;
exit;
end;
if NN=0 then
begin
HZ:='零元';
result:=HZ;
exit;
end;
NS:='零壹贰叁肆伍陆柒捌玖';
NW:='分角元拾佰仟万拾佰仟亿拾佰仟万';
NN:=StrToFloat(FormatFloat('0.00',NN));//这句是经过网友jycjd调试得出的结果
NA:=FloatToStr(NN*100);
LA:=length(NA);
X:=1;
HZ:='';
while X<=LA do
begin
NK:=Ord(NA[x])-Ord('0');
N1:=Copy(NS,NK*2+1,2);
N2:=Copy(NW,LA*2+1-X*2,2);
if (NK=0) AND ((N2='亿') OR( N2='万') OR( N2='元'))then
begin
if copy(HZ,Length(HZ)-1,2)='零' then
HZ:=copy(HZ,1,length(HZ)-2);
if copy(HZ,Length(HZ)-1,2)='亿' then
if N2='元' then
begin
N1:=N2;
N2:='零';
end
else
N2:=''
else
begin
N1:=N2;
N2:='零';
end
end
else if NK=0 then
begin
if copy(HZ,length(HZ)-1,2)='零' then
N1:='';
if N2='分' then
begin
if copy(HZ,length(HZ)-1,2)='零' then
HZ:=copy(HZ,1,length(HZ)-2)+'整'
else
HZ:=HZ+'整';
N1:='';
end;
N2:='';
end;
HZ:=HZ+N1+N2;
X:=X+1
end;
Result:=HZ;
end;
wangyise1 2001-10-11
  • 打赏
  • 举报
回复
在这里写出来不好吗?
first_lover 2001-09-11
  • 打赏
  • 举报
回复
WilliamGui(寒锋):可以把你的方法发到我的信箱里面去吗?frankboy1@sina.com,不胜感激.也欢迎其他朋友赐教!
cobi 2001-09-10
  • 打赏
  • 举报
回复
gz
WilliamGui 2001-09-10
  • 打赏
  • 举报
回复
太對了﹐我寫了有一個小的公共程序﹐一定滿足你的要求
轉換的非常正確
williamgui@21cn.com

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧