Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

TFloatfields ??

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
westy100697
Asked:
westy100697
  • 2
  • 2
1 Solution
 
florisbCommented:
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
 
westy100697Author Commented:
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
 
westy100697Author Commented:
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
 
florisbCommented:
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
 
jeurkCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now