例如
a:float;
a:=12.123456
我要取小数位后四位
有两种情况
1)要四舍五入 返回12.1235
2)不要四舍五入 返回12.1234
这两种情况怎么做到?

解决方案 »

  1.   

    math单元的RoundTo,四舍五入
    function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
    var
      LFactor: Double;
    begin
      LFactor := IntPower(10, ADigit);
      Result := Round(AValue / LFactor) * LFactor;
    end;不四设五入把那Round替成Truncate
      

  2.   

    a:=a * 10000;1.a:=trunc(a);
    2.a:round(a);a:=a / 10000;
      

  3.   

    自己写个函数吧.
    function FormatFloat(f:double):double
    begin
       Result :=strToFloat(Fromdsat('%8.2f',[f]));
    end;
      

  4.   

    哦,是
     
      自己写个函数吧. 
    function FormatFloat(f:double):double 
    begin 
    Result :=strToFloat(Format('%8.2f',[f])); 
    end; 
      

  5.   

    这么累做什么?
    可以用Round !!
    但,我想你为何不将数据库中的类型转换为Numeric(19,4),
    这样在向数据库中保存的时候,它会自然将你所保存的数据截为小数位后四位的
      

  6.   

    用RoundTo函数
    RoundTo(2.57654,-2)=2.58
      

  7.   

    用Format函数,它会自动处理四舍五入,返回string
      

  8.   

    用frac可以取得小数部分另,trunc用来取得整数部分