BCD and currency types in Delphi

Source: Internet
Author: User



BCD is binary-coded decimal type. in Delphi, the BCD field type can accurately store the floating point data type.

The data type of the BCD code supported by Delphi is tbcd. Its definition is as follows:

Tbcd = packed record

Precision: byte; {1 .. 64}

Signspecialplaces: byte; {sign: 1, Special: 1, places: 6}

Fraction: packed array [0 .. 31] of byte; {BCD nibbles, 00 .. 99 per byte, high nibble 1st}


BCD is supported in the fmtbcd unit. Therefore, to use the BCD function, you must reference this unit.

The BCD functions of Delphi include:


Calculate the sum of two BCD codes


Compare the two BCD sizes


Division of BCD data


BCD data Multiplication


Returns the number of BCD data records. For example, if BCD 123 returns 3, and BCD 9382 returns 4.


Returns the decimal places of BCD codes.


Subtract Two BCD codes


Convert BCD code to current data type


Data Type converted from BCD to double


Data Type converted from BCD to integer


BCD code to a string


Convert BCD code to a string with format Control


Current data type conversion to BCD code


Convert double data type to BCD code


Format the BCD code as a string


Convert Integer type to BCD code


Determine whether BCD is negative


Converts a BCD value to another BCD value based on the given precision and decimal places.


Determine whether BCD is null


Convert string to BCD code


If the string is converted to a BCD code, the default value is returned if the conversion fails.


The same as the money type in SQL Server, the currency type in Delphi:

1) occupies 8 bytes.

2) always four decimal places.

3) range:-2 ^ 63 ~ 2 ^ 63-1 (-922,337,203,685,477.5808 ~ 922,337,203,685,477.5807 ). The storage format is always multiplied by 10000, and then saved in integer format.

 III.BCDField Type (Tbcdfield)

Currently, many databases have decimal and numeric data types. They can precisely store floating point data and map decimal and numeric data types to BCD fields.

In the tdatabase control of BDE, there is an enablebcd option:

The enablebcd option is also available in ADO tadoquery.



The enablebcd option describes how to process numeric fields (decimal and numeric:

1) When enablebcd is true, the field ing of the numerical type is a tbcdfield class.

2) When enablebcd is set to false, the numeric field is mapped to the tfloatfield class.

Tbcdfield is defined in the DB. Pas file:

Tbcdfield = Class (tnumericfield)


Fcurrency: Boolean;

Fcheckrange: Boolean;

Fminvalue: currency;

Fmaxvalue: currency;

Fpretries: integer;

Procedure setcurrency (value: Boolean );

Procedure setmaxvalue (value: currency );

Procedure setminvalue (value: currency );

Procedure setprecision (value: integer );

Procedure updatecheckrange;


Class procedure checktypesize (value: integer); override;

Procedure copydata (source, DEST: pointer); override;

Function getasbcd: tbcd; override;

Function getascurrency: currency; override;

Function getasfloat: Double; override;

Function getasinteger: longint; override;

Function getasstring: string; override;

Function getasvariant: variant; override;

Function getdatasize: integer; override;

Function getdefaultwidth: integer; override;

Procedure gettext (VAR text: string; displaytext: Boolean); override;

Function getvalue (VaR value: currency): Boolean;

Procedure setasbcd (const value: tbcd); override;

Procedure setascurrency (value: currency); override;

Procedure setasfloat (value: Double); override;

Procedure setasinteger (value: longint); override;

Procedure setasstring (const value: string); override;

Procedure setvarvalue (const value: variant); override;


Constructor create (aowner: tcomponent); override;

Property Value: currency read getascurrency write setascurrency;


{Lowercase to avoid name clash with C ++ currency type}

Property currency: Boolean read fcurrency write setcurrency default false;

Property maxvalue: currency read fmaxvalue write setmaxvalue;

Property minvalue: currency read fminvalue write setminvalue;

Property precision: integer read fpresponwrite setprecision default 0;

Property size default 4;


Because tbcdfield uses the currency type to store data (Note: It is not stored using tbcd), and currency is fixed with only four decimal places, therefore, if the precision exceeds 4 bits, use tfloatfield (enablebcd BIT false ).


  1. Direct support for BCD codes in Delphi.



BCD and currency types in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.