Solved

TFloatfields  ??

Posted on 2000-03-22
5
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now