Solved

TFloatfields  ??

Posted on 2000-03-22
5
306 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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