BCD and currency types in Delphi

Source: Internet
Author: User

DelphiMediumBCDAndCurrencyType

 I.BCDType

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}

End;

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

The BCD functions of Delphi include:

Bcdadd

Calculate the sum of two BCD codes

Bcdcompare

Compare the two BCD sizes

Bcddivide

Division of BCD data

Bcdmultiply

BCD data Multiplication

Bcdprecision

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

Bcdscale

Returns the decimal places of BCD codes.

Bcdsubtract

Subtract Two BCD codes

Bcdtocurr

Convert BCD code to current data type

Bcdtodouble

Data Type converted from BCD to double

Bcdtointeger

Data Type converted from BCD to integer

Bcdtostr

BCD code to a string

Bcdtostrf

Convert BCD code to a string with format Control

Currtobcd

Current data type conversion to BCD code

Doubletobcd

Convert double data type to BCD code

Formatbcd

Format the BCD code as a string

Integertobcd

Convert Integer type to BCD code

Isbcdnegative

Determine whether BCD is negative

Normalizebcd

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

Nullbcd

Determine whether BCD is null

Strtobcd

Convert string to BCD code

Trystrtobcd

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

 II.CurrencyType

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)

Private

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;

Protected

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;

Public

Constructor create (aowner: tcomponent); override;

Property Value: currency read getascurrency write setascurrency;

Published

{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;

End;

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 ).

4.References

  1. Direct support for BCD codes in Delphi.

Http://www.cnblogs.com/ywangzi/archive/2012/11/14/2769823.html

 

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.