Solved

TFloatfields  ??

Posted on 2000-03-22
5
298 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
[X]
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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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