当前位置:编程学习 > Delphi >>

delphi为什么没有四舍五入函数?

补充:我知道round(x)这个函数,它是四舍六入,只对偶数取整的,但为什么没四舍五入函数呢,我纳闷了
答案:我倒觉得用format不错:
format('15.2f%',表达式)
15表示总位数,2表示小数点后的位数!
其他:谁说没有,只是你不知道而以
round(x)舍入函数;将实数x四舍五入得到最接近的整数,例;round(3.3)=3,round(-8.9)=-9;
不懂的多百度下吧, round()函数有个缺点如下:round(5.5)=6 ,round(6.5)=6
也就是说当最后一位为5时,四舍五入的进位与否与前一位的奇偶有关,故为避免这种情况最好还是自己写 ,下面是本人写的,有点罗嗦,希望能帮到你
//四舍五入保留小数点后len位数
function Tsys_njqlrb_f.RoundClassic(R: Real;len:integer): real;
var
  i,j:integer;
  str:string;
begin
  if Trunc(R)=R then
  begin
    Result:= Trunc(R);
    exit;
  end;
  Result:= Trunc(R);
  str:=(copy(floattostr(R),length(inttostr(Trunc(R)))+2,len));
  str:='0.'+str;
  Result:=Result+strtofloat(str);
  j:=1;
  for i:=1 to len do  j:=j*10;
  if length(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1))<>0 then
  begin
    if strtoint(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1)) >= 5 then
      Result:= Result + 1/j;
  end;
end; round是四舍六入五留双 

上一个:delphi中怎样把这一串十六进制的字符串906909FF78FF0D0A转变成十进制字符串
下一个:请问delphi 怎么修改汇编上的一个eax的值

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,