Currency type and tcurrencyfiled tragedy

Source: Internet
Author: User

Due to program problems in the past two days, the user's settlement amount is often more than a few digits after the decimal point. You don't have to think about it because the floating point accuracy is not accurate.

Check that the data type in the program uses currency. According to the description of the data type, the amount type should be stored in the real number format, and there will be no inaccurate precision problem at all, why is there an inexplicable decimal number in reality?

The tracking program continues and finds that the memory table control is used during data transfer, and the field type corresponding to the amount data is exactly tcurrencyfiled

Write a test code and find that each time data is accessed from the memory table field, a numerical error occurs. Is there a problem with this field?

After tracking the source code, we found that tcurrencyfiled was inherited from the tdoublefield field. Does this field use double-format data storage ??!!

 

Let's take a look at help, and then the tragic discovery that people have already told you:

TCurrencyField encapsulates the fundamental behavior common to currency fields. TCurrencyField differs from its immediate ancestor TFloatField only in having a DataType of ftCurrency, and in formatting the value using a currency format (one that represents monetary values) by default. Currency fields can hold values in the range from (positive or negative) 5.0 * 10^-324 to 1.7 * 10^308 with an accuracy of 15 digits. Do not confuse TCurrencyField with the Currency data type. Currency fields use the double data type to store and manipulate their values. This data type is the format used by the physical database tables for currency fields. The TBCDField class uses the Currency data type to store and manipulate its value.If you use the Fields editor at design time to create a persistent field component for the currency field, you can access it by name at runtime. When using dynamic field components, you can access the TCurrencyField instance using the dataset?s Fields property or FieldByName method.

 

I just wiped the data... currency data type, instead of tcurrencyfield, it should be tbcdfield...

 

Finally, let's add: Generally, our common basic data types are not mentioned. They are basically classified into integer and floating-point data types, where integer data is stored in real numbers, and each data bit represents the exact number, the floating point is stored in an exponential manner. It depends on the precision you require.

So... real-number storage can be directly equal, while indexes cannot be compared (not absolutely incomparable)

The most special one here should belong to the currency type. Data Representation should belong to the floating point type because it has decimal places. In terms of storage form, it also belongs to Real-number Storage, because each data bit of it represents the exact value.

If you still do not understand, you can use the following code to test:

var  i64: Int64;  c: Currency;begin  i64 := 0;  c := 123 / 7;  Move(c, i64, Sizeof(c));  ShowMessage(CurrToStr(c) + #13#10 + IntToStr(i64));end;

The result is as follows:

17.5714

175714

 

You understand, currency actually stores the same data as int64, but when using it, it considers the last four digits as decimal places.

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.