这不是Bug,因为Currency的定义中小数部分只有4位,最后一位自然会丢失了。

解决方案 »

  1.   

    Round45函数中Currency换做double,问题就更严重了,结果好像跟round结果一样???在线等候...
      

  2.   

    真正的原因是计算机对浮点数储存的问题。事实上类似0.005这样的数在获得最佳精度的时候为0.0049999999999xxx。
    如果要获得0.005的小数点后2位舍入结果,我认为应该这样操作:float x = 0.005
    FormatFloat("0.000",x);
    float s = Round(x*100)/100;
      

  3.   

    瞧定义:
    Currency is a fixed-point data type that minimizes rounding errors in monetary calculations. It is stored as a scaled 64-bit integer with the four least-significant digits implicitly representing decimal places. When mixed with other real types in assignments and expressions, Currency values are automatically divided or multiplied by 10000.好事做到底,我帮你把 Round45 改了一下,现在精确到8位都没问题了
      

  4.   

    这是我写的一个函数,你试试。double DOLPHI_Round( double lX )
    {
        double  lS;
        int     iS = floor(lX*1000);    if( iS%10 >= 5 )
        {
            lS = (double)(iS/10+1)/100;
        }
        else
        {
            lS = (double)(iS/10)/100;
        } 
        return lS;
    }
      

  5.   

    这是更新的,自定义小数点后几位double DOLPHI_Round( double lX ,int iLength)
    {
        int iMul = 1;
        for(int i=;i<iLength-1;i++)
        {
            iMul *= 10;
        }
        double  lS;
        int     iS = floor(lX*iMul*10);    if( iS%10 >= 5 )
        {
            lS = (double)(iS/10+1)/iMul;
        }
        else
        {
            lS = (double)(iS/10)/iMul;
        } 
        return lS;
    }
      

  6.   

    function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;注意类型匹配问题。Expression     ValueRoundTo(1234567, 3)   1234000
    RoundTo(1.234, -2)     1.23
    RoundTo(1.235, -2)     1.24
    RoundTo(1.245, -2)     1.24
      

  7.   

    解决问题,将Round45的Currency改成Double类型就可以了。这里真是个好地方!!!给分啦// 实数取四舍五入,使用 myCurrency:=Round45(myCurrency,2)
    function Round45_EX(GetOldCurr: Double;Digits: integer=2): Double;
    var tmpCurr:Double;
    begin
      tmpCurr:= GetOldCurr * (IntPower(10,Digits));  // 小数点右移digits位
      if Abs(Frac(tmpCurr)) < 0.5 then
        tmpCurr:= Int(tmpCurr)                // 保留整数部分
      else
      if tmpCurr>0 then
        tmpCurr:= Int(tmpCurr)+1
      else tmpCurr:= Int(tmpCurr)-1;          // 进位
      Result:= tmpCurr /IntPower(10,Digits);  // 小数点回移
    end;
      

  8.   

    RoundTo 在MATH单元中声明。手动添加即可。-N表示小数位数N。
      

  9.   

    RoundTo没有在math.pas文件中啊,我的是delphi5,不会delphi6才有吧
      

  10.   

    转摘D6 帮助文档。Rounds a floating-point value to a specified digit or power of ten using “Banker’s rounding”.Unit
    Math
      

  11.   

    有谁能贴一下Delphi6.0中RoundTo的源代码吗???
      

  12.   

    function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
    var
      LFactor: Double;
    begin
      LFactor := IntPower(10, ADigit);
      Result := Round(AValue / LFactor) * LFactor;
    end;另外,Round函数在Delphi的帮助中已经说得很明白,是四舍六入,五取双
      

  13.   

    我常用float s = Round(x*100+0.5)/100;来做
      

  14.   

    我常用
    float s = Round(x*100+0.5)/100
    来做
      

  15.   

    function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
    好像也解决不了问题吧?
      

  16.   

    function roundTo(s: double; digits: integer): double; //s, digits > 0
    var
      ss: string;
      p: integer;
    begin
      str(s : 0 : 10, ss);
      p := pos('.', ss) + digits + 1;
      result := StrToFloat(copy(ss, 1, p - 1));
      if ss[p] >= '5' then
        result := 1 / IntPower(10, digits) + result;
    end;
      

  17.   

    我确信D6的RoundTo对此问题不能解决!
    我试一试aliceZooZ的看!
      

  18.   

    我用D5,ROUNDTO还没用过,而且ROUNDTO从源码来看,好象也没解决四舍五入的问题。
    唉,没有D6,只能关注了。
      

  19.   

    贴——D6对ROUNDTO的定义(math.pas):
    function TForm1.myRoundTo(const AValue: Double;
      const ADigit: TRoundToRange): Double;
    var
      LFactor: Double;
    begin
      LFactor := IntPower(10, ADigit);
      Result := Round(AValue / LFactor) * LFactor;
    end;
      

  20.   

    我确信D6的RoundTo对此问题不能解决!
      

  21.   

    UnitMathCategoryArithmetic routinesfunction IntPower(const Base: Extended; const Exponent: Integer): Extended register;DescriptionIntPower raises Base to the power specified by Exponent.
      

  22.   

    ※ 引述《YILI0429》於 08/30/2001 03:19:06 發表之銘言:
    >1."mod"這個指令只能下在integer上...
    >但是我現在有須要用一個float去除一個整數...
    >然後只求它的餘數...??????????
    >
    >2.我現在要讀一個float的整數部份...
    >(ie.想要float to integer)
    >我該怎麼辦..??!!
    >
    >新手上路請不吝指教...!!function Round(X: Extended): Int64;
    function Trunc(X: Extended): Int64;Round 为四舍五入,Trunc 为舍整
      

  23.   

    如果用MS SQL SERVER 或 ORACLE
    这种处理理应在数据库定义时处理,这样在数据更新时底层自动无误进行快速处理。(用触发器比较好)。如果一定要在前端处理的话我也没意见。
      

  24.   

    YANGYUGW(yangyu)你的帖子,好像没有直接关系啊
      

  25.   

    其实自己写一个就是最好不过了,可以不考虑数据类型function MyRound(x:real,b:Integer):real;
    begin
      ....
    end;
      

  26.   

    leemingsong(陨石) 你不是已经解决了吗?“解决问题,将Round45的Currency改成Double类型就可以了。”现在还有什么问题呢?你收到我发的消息了吧?