Recent

Author Topic: TFloatField  (Read 1328 times)

fmc

  • New Member
  • *
  • Posts: 37
TFloatField
« on: November 04, 2019, 03:55:43 am »
Every time REAL numbers are read from the db and shown in the dbgrid, I have to convert them from scientific notation:
Code: Pascal  [Select][+][-]
  1. TFloatField(DM1.ZQ_AddItems.FieldByName('inventory_cost')).DisplayFormat := '####0.00';
  2. TFloatField(DM1.ZQ_AddItems.FieldByName('inventory_sell_price')).DisplayFormat := '####0.00';
  3. TFloatField(DM1.ZQ_AddItems.FieldByName('inventory_margin_contribution')).DisplayFormat := '####0.00';

This is a small thing to do, but I was wondering if there's another way. 
Win X Pro / Lazarus 2.0.6 / FPC 3.0.4

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: TFloatField
« Reply #1 on: November 04, 2019, 06:27:15 am »
Create your own floatfield?
Code: Pascal  [Select][+][-]
  1. Type
  2.   TMyFloatField = class(TfloatField)
  3.   public
  4.     Constructor Create(AOwner: TComponent);override;
  5.   end;
  6.  
  7.   Constructor TMyFloatFielld.Create(AOwner: TComponent);
  8.   begin
  9.     inherited Create(AOwner);
  10.     SetDisplayFormat ('###0.00');
  11.   end;

You probably need to Register the class as well to use it beyond using it for casting
Another way is to use attributes, although that is more complex than the above and needs a recent trunk.
« Last Edit: November 04, 2019, 06:52:14 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

tonyw

  • Sr. Member
  • ****
  • Posts: 321
    • MWA Software
Re: TFloatField
« Reply #2 on: November 04, 2019, 12:38:00 pm »
TFloatField just uses the RTL FloatToStr function to format real numbers as text and which is not always that user friendly.

For float fields, I would normally use the fields editor to add the dataset fields to the form and then define an "OnGetText" event handler for each field for which you need to override the default formating. The event handler could be as simple as:

procedure TMyForm.MyDataSetFieldNameGetText(
  Sender: TField; var aText: string; DisplayText: Boolean);
begin
  if DisplayText then
    aText := FormatFloat('####0.00',Sender.AsFloat)
 else
  aText := Sender.AsString
end;

The great thing about event handlers is that you can use the same event handler for every field that you want to format the same way.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: TFloatField
« Reply #3 on: November 04, 2019, 02:37:39 pm »
@tonyw
That does not lead to reduced code and that was the question.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: TFloatField
« Reply #4 on: November 04, 2019, 03:42:49 pm »
If the event handler is obviously accessible \ public (in the TForm, or in a central unit) from its datamodule-business-object, then the code is serialized (potentially between all its TFloatFields), and so is reduced. But it's less smart than including it through an inheritance, and he'll have always to remember that it exists... somewhere :) .
« Last Edit: November 04, 2019, 04:06:41 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018