Recent

Author Topic: [SOLVED] DBEdit - TDbf Float field  (Read 1328 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 436
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
[SOLVED] DBEdit - TDbf Float field
« on: May 08, 2024, 06:18:36 pm »
What's the best way to allow the user to edit a float field (TDbf Level 7)? In my SalesOrder.dbf file I have 2 fields that are type float. One is Commission and the other is Price. The size for both fields are 6 with decimal as 2. Just entering values for a couple of records with DBase Manager the data is displaying correctly for 500.00 and 1000.00 for the PRICE fields and 25.00 and 23.00 for the COMMISSION fields.
But in my Laz app Sales Order form I have 2 DBEdit's for these 2 fields.
For the values 500.00 and 1000.00 my DBedit's are displaying 5E2 and 1E3. I have a mask defined for both DBEdit's as 9999.99;1;_
Would anyone be able to advise the correct way to define the DBEdit's properly?
« Last Edit: May 11, 2024, 03:57:48 am by 1HuntnMan »

wp

  • Hero Member
  • *****
  • Posts: 13398
Re: DBEdit - TDbf Float field
« Reply #1 on: May 08, 2024, 08:09:07 pm »
In my SalesOrder.dbf file I have 2 fields that are type float. One is Commission and the other is Price. The size for both fields are 6 with decimal as 2.
How do you specify the size and decimal count for these fields?

I would not touch the Size and Precision properties in the dbf FieldDefs because they behave in a counter-intuitive way.
  • Size is the total count of characters before the decimal point, counted from the right, e.g. when Size=3 and the entered value is 1234, the DBF will store 234! When the Size is too small the format switches to exponential notation, like in your case.
  • And Precision is not the number of decimal places, but the total number of digits: 1234.12345 is displayed as 1234.1 when Precision is 5, but 0.12345678 will become 0.12345 with the same precision.
Not sure if other dataset types behave in the same way.

Since you want a constant count of decimal places I would not recommend to change the default settings, i.e. add float fields only by Dbf1.AddFieldDefs(fieldname, ftFloat), no further specification.

The way to get two fixed decimal places is via the DisplayFormat and EditFormat of the field which you specify as '0.00' for two decimals. After opening the dbf add some code which iterates over the ftFloat fields, cast them to TFloatField and set the DisplayFormat/EditFormat:
Code: Pascal  [Select][+][-]
  1.   Dbf1.Open;
  2.   for i := 0 to Dbf1.FieldCount-1 do
  3.   begin
  4.     if Dbf1.Fields[i] is TFloatField then
  5.       TFloatField(Dbf1.Fields[i]).DisplayFormat := '0.00';
  6.   end;  

« Last Edit: May 08, 2024, 08:11:30 pm by wp »

Nicole

  • Hero Member
  • *****
  • Posts: 1308
Re: DBEdit - TDbf Float field
« Reply #2 on: May 08, 2024, 08:10:33 pm »
may be something like this may help (part of my code)

Code: Pascal  [Select][+][-]
  1.    IBTable_Kontoinfo.Active:=true;
  2.   TBCDField(IBTable_Kontoinfo.FieldByName('RISK_BETRAG')).EditFormat := '#.00';
   

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 436
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: DBEdit - TDbf Float field
« Reply #3 on: May 11, 2024, 03:57:11 am »
Okay, great! This really helps plus I found a doc online called Beginners Guide for DBase Tables. Thanks, makes sense now.

 

TinyPortal © 2005-2018