使用下面语句怎么就是得不到2.97数据的呢?
showmessage(floattostr(round(1*2.964446*100)/100));
如何将2.964446使用四舍五入的方法得到2.97呢?

解决方案 »

  1.   

    showmessage(floattostr(round(1*2.964446*100+1)/100));
      

  2.   

    roundto(1.235,-2)返回1.24roundto(1.935,-2)返回1.93统计算法四舍六入五看前
      

  3.   

    用delphi的round函数四舍五入就是存在 ZyxIp(绝望中...) 的问题,可以采用其解决。a:=Int(2.96445*100)=296
    b:=2.96445*100-a=0.445.
    然后通过判断b是否大于0。5来进行四舍五入。这样准确,不会出现ZyxIp(绝望中...) 的问题。
      

  4.   

    SetRoundMode()    <----可以设置四舍五入的模式--------Sets the FPU rounding mode.UnitMathCategoryFPU controltype TFPURoundingMode = (rmNearest, rmDown, rmUp, rmTruncate);
    function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;DescriptionCall SetRoundingMode to specify how the FPU handles rounding issues. The rounding mode can be any of the following values:Value MeaningrmNearest Rounds to the closest value.
    rmDown Rounds toward negative infinity.
    rmUp Rounds toward positive infinity.
    rmTruncate Truncates the value, rounding positive numbers down and negative numbers up.
      

  5.   

    用Ceil()求一个数的上限,用floor()求一个数的下限
      

  6.   

    你的语句改为即可:
    showmessage( floattostr( Ceil(1*2.964446*100) / 100 ) );
      

  7.   

    formatfloat(#.##,2.964446);
    formatfloat('#.##',2.964446);
    忘了要不要带引号了,你自己试吧。
      

  8.   

    var
      mynum:Double;
    mynum:=StrToFloat(FormatFloat('##.##',2.964446));
      

  9.   

    edit1.Text:=formatfloat('0.000',2.964446);记住得出的是string;
      

  10.   

    Trunc(x+0.5)就是四舍五入了。 例如 4.3+0.5=4.8,被Trunc掉之后是4,程序执行正确。4.5+0.5=5,被Trunc掉就是5,还是正确。
    对于取小数点多少位之后四舍五入,这样:
    Trunc(x*a+0.5)/a,这里a为1,10,100等10的n-1次方(n=1,2,3,...)。
    所以,你要取0.54663小数点后面3位并四舍五入,就这样:
    0.54663*100+0.5=546.63+0.5=547.13,Trunc之后时547,然后/100就成为0.547了.--------
    这个是CSDN上的摘录的。是哪位网友发的帖子我忘记了,但还是要感谢他的代码。
      

  11.   

    formatfloat('#.##',2.964566),保证没问题
      

  12.   


    使用下面语句怎么就是得不到2.97数据的呢?
    showmessage(floattostr(round(1*2.964446*100)/100));
    如何将2.964446使用四舍五入的方法得到2.97呢?************
    四舍五入怎么能得到2.97呢?四舍五入的规则都忘了吧.楼主?
      

  13.   

    2.96->3.00
    2.964->2.96
    以最后一位数字为基准!
      

  14.   

    我用
    FloatToStrF(); 函数 总体思路:
    ShowMessage(FloatToStrF(2.964446,ffFixed,15,5);//=2.96445 (先取小数点后五位)
    ShowMessage(FloatToStrF(2.96445,ffFixed,15,4);//=2.9645(再取小数点后四位)
    ShowMessage(FloatToStrF(2.9645,ffFixed,15,3);//=2.9644(再取小数点后三位)
    ShowMessage(FloatToStrF(2.965,ffFixed,15,2);//=2.97(再取小数点后二位)
    1、先找到小数点位置。
    2、倒着一个个的取数。
    //
    procedure TForm1.Button1Click(Sender: TObject);
    var
    d :double;
    i :integer;
    j :integer;
    s :string;
    begin
    d := 2.964446;
    s :=FloatToStr(d);
    i:=0;
    j :=0;
    i := pos('.',s);//找到小数点的位置.
    memo1.Lines.Clear;
    i := Length(s)-i;//取出小数据点后面有多少位小数.
    for j:=i-1 downto 2 do //到2是保留二位小数点.倒着取数.
    begin
     s := FloatToStrF(d,ffFixed,15,j); //Delphi 有问题,当J=4时,d=2.96445  结果应该 是 2.9645才对呀,但是在这个循还体中却为 2.9644 郁闷呀!    
    //要是不信你可以试试 ShowMessage(FloatToStrF(2.96445,ffFixed,15,4));//=2.9645 
     memo1.Lines.Add('s:='+s+' j:'+intToStr(j));
     d := StrToFloat(s);
     memo1.Lines.Add('d:= '+FloatTostr(d));
    end;
    caption :=s;
    end;
    //请高人指点迷津 
      

  15.   

    你用CEIL函数不行吗???????????
      

  16.   

    s := FloatToStrF(d,ffFixed,15,j); //Delphi 有问题,当J=4时,d=2.96445  结果应该 是 2.9645才对呀,但是在这个循还体中却为 2.9644 郁闷呀!    这个遵循"四舍六入五成双"的原则.
      

  17.   

    这个"四舍六入五成双"的原则.为什么在单步执行的情况下没有问题:
    ShowMessage(FloatToStrF(2.96445,ffFixed,15,4));//=2.9645 这样就是对的。
      

  18.   

    最终写了两个函数:希望对大家有一点点用吧!
    function RoundEx(const val: string; ipos: integer = 2): string; overload;
    function RoundEx(const val: double; ipos: integer = 2): double; overload;
    {-----------------------------------------------------------------------------
      Procedure: RoundEx
      Author:    Administrator
      Date:      18-八月-2003
      Arguments: const val: double; ipos: integer = 2
      Result:    double   取四舍五入的函数。
    -----------------------------------------------------------------------------}function RoundEx(const val: double; ipos: integer = 2): double;
    var
      dTemp: Extended;
      i, j: integer;
      str: string;
    begin
      result := val;
      dTemp := val;
      str := FloatToStr(dtemp);
      i := 0;
      j := 0;
      i := pos('.', str);
      i := Length(str) - i;
      if i <= ipos then //如果小数位数小于要取的位数,就直接在后面补0。
      begin
        str := FloatToStrF(dTemp, ffFixed, 15, ipos);
        result := StrToFloat(str);
        exit;
      end;
      for J := i - 1 downto ipos do
      begin
        str := FloatToStrF(dtemp, ffFixed, 15, j);
        dtemp := StrToFloat(str);
      end;
      result := dtemp;
    end;function RoundEx(const val: string; ipos: integer = 2): string;
    var
      dTemp: Extended;
      i, j: integer;
      str: string;
      fmt: TFormatSettings;
    begin
      result := val;
      dTemp := strToFloat(val);
      str := FloatToStr(dtemp);
      i := 0;
      j := 0;
      i := pos('.', str);
      i := Length(str) - i;
      if i <= ipos then //如果小数位数小于要取的位数,就直接在后面补0。
      begin
        str := FloatToStrF(dTemp, ffFixed, 15, ipos);
        result := str;
        exit;
      end;
      for J := i - 1 downto ipos do
      begin
        str := FloatToStrF(dtemp, ffFixed, 15, j);
        dtemp := strtoFloat(str);
      end;
      result := str;
    end;