RAD Studio VCL Reference
TBCDField Class

TBCDField represents a binary-coded decimal (BCD) field in a dataset.

TBCDField = class(TNumericField);
class TBCDField : public TNumericField;

TBCDField encapsulates the fundamental behavior common to binary-coded decimal (BCD) fields. BCD values provide greater precision and accuracy than floating-point numbers. BCD fields are often used for storing and manipulating monetary values.  

The IDE uses two different field types for representing BCD fields: TBCDField and TFMTBCDField. TBCDField uses the currency (Delphi) or System::Currency (C++) type to manipulate BCD values. This is faster than storing and manipulating the value using a true BCD type, but limits the precision of the BCD values it can support to 4 decimal places and 20 significant digits. 

TBCDField converts the data from a BCD value to a currency value when it fetches the data from the database table, and converts it from a currency value to a binary-coded decimal value when it posts the data. If the underlying database table contains a value that requires greater precision, TBCDField raises an exception. If your application requires BCD values with more than 4 decimal places or 20 significant digits, you should use TFMTBCDField instead. TFMTBCDField is a true BCD, with the precision of the binary-coded decimal type (TBCD) but with somewhat slower performance. 

If you use the Fields editor at design time to create a persistent field component for the BCD field, you can access it by name at runtime. When using dynamic field components, you can access the TBCDField instance using the dataset's Fields property or FieldByName method.  

Delphi Examples: 


This example requires a button, a test edit, and a populated
ClientDataSet.  Pipe the ClientDataSet through a DataSource
to a DGGrid or DBNavigator to control the current field.
Cast the data correctly according to the field type when
assigning to the test edit.

uses SwSystem;

procedure TForm1.Button1Click(Sender: TObject);
var MyBuffer: Pointer;
{ Retrieve the "raw" data from Field1 }
with CDS.Fields[0] do
    if not IsBlob then { this does not work for BLOB fields }
      { Allocate space }
      MyBuffer:= GetMemory(DataSize);
        if not GetData(MyBuffer) then
          MessageDlg(DisplayName + ' is NULL', mtInformation, [mbOK], 0)
          { Do something with the data };
          Edit1.Text:= string(PAnsiChar(MyBuffer)); // for a stringfield
        { Free the space }
        FreeMem(MyBuffer, DataSize);

procedure TForm1.FormCreate(Sender: TObject);
  CDS.LoadFromFile(GetCurrentDir + '\CDS.XML');
  CDS.LoadFromFile(gsAppPath + 'CDS.XML');


Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!