Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TFloatfields  ??

Posted on 2000-03-22
5
297 Views
Last Modified: 2010-04-04
Hi,
   This is just a general question about Delphi TFloatfields. I am just wondering why if I set a field to

Field Name  Type Size  Decimals

MYFIELD      F     6       2


that if I have a number stored in this field as  21.30 for example and then read that into a Real variable i get the following.

If I do Frac(21.30) I find the result to be 0.299999999. Why is that if I have specified the 2 decimal places in the database table. Where does this problem originate from ?? As you could understand this becomes a problem if I use a rounding function as it rounds down rather that up. For example if I round 21.30 in Delphi to the nearest 0.2 I get 21.2 rather that 21.4.

I know setting the field type as TBCDField will fix the problem but I want some discussion on how this comes to be. Any of the Gurus got an answer to this one ?

Regards,

 Westy :)
0
Comment
Question by:westy100697
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:florisb
ID: 2648885
see help; datatype and meaning of size:

ftFloat: Size is not used; its value is always 0.

Decumals; use precision:
property Precision: Integer;

Description

Use Precision to set the number of digits beyond the decimal point that the value should be formatted to before rounding begins. Precision affects the format of the floating-point value when it is displayed as a text string, not the actual floating-point value that is stored in the field.

The default value of Precision is 15 decimal places.

Note:      Precision must be greater than or equal to 2. Trying to set Precision to a value less than 2 changes the value of Precision to 2.

Floris.
0
 

Author Comment

by:westy100697
ID: 2651066
florisb,
        I know all of that but if you have a look in the BDE and setup a dbase for Windows ALIAS. There is an option in there called EnableBCD. Have a look at the help on that. I am just wondering why Borland would go with something that is not the most accurate way of storing data even if it is a little slower. Have a look yourself and you will see what I mean.

WESTY :)
0
 

Author Comment

by:westy100697
ID: 2651071
Here is a snippet from that help file

ENABLE BCD      Specifies whether BDE translates numeric and decimal fields into floating point values or binary coded decimal (BCD) values. BCD values eliminate the rounding errors associated with floating point math (such as a 3 * (2/3) resulting in 2.00000000001). When ENABLE BCD is set to TRUE, DECIMAL and NUMERIC fields are converted to BCD.

WESTY :)
0
 
LVL 2

Expert Comment

by:florisb
ID: 2652514
I don't know about those BDE settings, I don't think I can really help you.

Using TFloatfields with a correct precision and skipping those BDE settings should work fine. I whould use masks /precision / whatever in the GUI. Never had problems with incorrect rounding of the BDE.

Working with ADO now....:-)....no complaints.

Floris.
0
 
LVL 4

Accepted Solution

by:
jeurk earned 25 total points
ID: 2654927
Hello,
Borland has kept all that bcd stuff for
compatibility reasons. The rouding problems comes from the internal storage technic of integer and floats.
set enable bcd to true is not the solution.
The solution is to use Tcurrency fields ;)

CU
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 57
Magic Software info 18 138
Controlled Assessment GCSE - desperate help needed 4 102
Base1 Encode/Decode 3 82
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question