delphi中有四舍五入函数吗?(25分)

  • 主题发起人 主题发起人 mxsbt2
  • 开始时间 开始时间
下面是保岛国整数的四舍五入函数,如果要保岛国n位小数,只需简单修改。
function myround(x : extended) : extended;
begin
if (int(x)*10+5) > int(x*10) then
begin
result := floor(x);
end else
begin
result := ceil(x);
end;
end;
 
它四舍五入是行,不过我想要的是按任意位数小数四舍五入
 
function myround(x : extended,y:integer) : extended;
integer z;
begin
z=1;
while y>1 do
begin
z=z*10;
y=y-1;
end
result=int((x*z+0.5)/z);
end;
 
请大家注意,round只能做五舍六入!!!!
 
FloatToStrF(StrToFloat(a),ffFixed,n,m)
 
FloatToStrF(StrToFloat(a),ffFixed,n,m)
是可以的
但好像的先用Trunc('float'*100+0.5)/100
 
FloatToStr(int((a)*100)/100)
 
Just try the function FormatFloat.Its define is:
function FormatFloat(format:string; arc:real):string;
for example:
r1:=FormatFloat('00.00',100); //r1:=100.00
r2:=FormatFloat('00',0.8); //r2:=1
r3:=FormatFloat('00.0',100.123456789); //r3:=100.1
r4:=FormatFloat('00.0000'.100.123456789); //r4:=100.1234
for most infomation, you can turn to delphi online help.
It's just my suggest, you can agree with it,or do nothing with it!
 
formate('%.2f',[youfloat]);
 
用format函数格式化就可以了
 
zwma:你的函数如何处理负数?
 
后退
顶部