ClientDataSet TBCDField rounding

左心房为你撑大大i 提交于 2019-12-11 19:48:19

问题


I'm using Delphi 5 + BDE + Oracle. I have the following function:

class function TClientDataSetFactory.GetClientDataSet(
      const qryGen: TDataSet): TClientDataSet;
    var
       dspDados: TDataSetProvider;
    begin
       Result := nil;
       try
          try
             Result := TClientDataSet.Create(nil);
             dspDados := TDataSetProvider.Create(Result);
             dspDados.DataSet := qryGen;
             qryGen.Active := True;
             qryGen.First;

             Result.Data := dspDados.Data;

             Result.First;
          except
             on E: Exception do
             begin
                raise;
             end;
          end;
       finally
       end;
    end;

so, when a run this:

var
   qryGen: TQuery;
   cdsGen: TClientDataSet;
begin
   qryGen := nil;
   try
      try
         qryGen := CriaQuery();
         qryGen.SQL.Text :=
            'SELECT SUM(TOTAL) AS TOTAL FROM MYTABLE';
         cdsGen :=  TClientDataSetFactory.GetClientDataSet(qryGen);
         ShowMessageFmt('Total: %f', [cdsGen.FieldByName('TOTAL').AsFloat]);
      except
         on E: Exception do
         begin
            raise;
         end;
      end;
   finally
      if Assigned(qryGen) then FreeAndNil(qryGen);
   end;
end;

i got "159,00" but, if i run this:

ShowMessageFmt('Total: %f', [qryGen.FieldByName('TOTAL').AsFloat]);

i got "159,25".

can someone help me?


回答1:


I solved the problem with another solution.

type
   TInternalQuery = class(TQuery)
   protected
      procedure InternalInitFieldDefs; override;
   public
      constructor Create(AOwner: TComponent; const qryGen: TQuery); reintroduce;
   end;

constructor TInternalQuery.Create(AOwner: TComponent; const qryGen: TQuery);
var
   intCont: Integer;
begin
   inherited Create(AOwner);
   Self.DatabaseName := qryGen.DatabaseName;
   Self.UpdateObject := qryGen.UpdateObject;

   Self.SQL.Text := qryGen.SQL.Text;

   for intCont := 0 to Self.ParamCount - 1 do
   begin
     Self.Params[intCont].Value := qryGen.Params[intCont].Value;
   end;  
end;

procedure TInternalQuery.InternalInitFieldDefs;
var
   intCont: Integer;
begin
   inherited InternalInitFieldDefs;
   for intCont := 0 to FieldDefs.Count - 1 do
   begin
      if (FieldDefs[intCont].Size = 0) and (FieldDefs[intCont].DataType = ftBCD) then
      begin
         FieldDefs[intCont].Precision := 64;
         FieldDefs[intCont].Size := 32;
      end;  
   end;  
end;

the problem is ((FieldDefs[intCont].Size = 0) and (FieldDefs[intCont].DataType = ftBCD)). when ClientDataSet is created, the field is truncated, because when oracle has a function like "SUM(TOTAL)" the result field is created with size 0, so the clientdataset handle the field as Integer field.




回答2:


Try with

ShowMessageFmt('Total: %n', [cdsGen.FieldByName('TOTAL').AsFloat])

or this

cdsGen := TClientDataSetFactory.GetClientDataSet(qryGen); **(cdsGen.FieldByName('Total') as TFloatField).DisplayFormat := '0.00';** ShowMessageFmt('Total: %f', [cdsGen.FieldByName('TOTAL').AsFloat])



来源:https://stackoverflow.com/questions/22303298/clientdataset-tbcdfield-rounding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!