TFloatField lazarus / 16-Databases ---------------------------------------------------------------------- fmc · 2019-11-04 02:55 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TFloatField (DM1.ZQ_AddItems.FieldByName('inventory_cost')).DisplayFormat := '#### 0.00';TFloatField(DM1.ZQ_AddItems.FieldByName('inventory_sell_price')) .DisplayFormat := '####0.00';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. ---------------------------------------------------------------------- Thaddy · 2019-11-04 05:27 Create your own floatfield? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Type  TMyFloatField = class(TfloatField)  public    Constructor Create(AOwner: TComponent);override;  end;   Constructor TMyFloatFielld.Create(AOwner: TComponent);  begin    inherited Create(AOwner);    SetDisplayFormat ('###0.00');  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. ---------------------------------------------------------------------- tonyw · 2019-11-04 11:38 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 · 2019-11-04 13:37 @tonyw That does not lead to reduced code and that was the question. ---------------------------------------------------------------------- devEric69 · 2019-11-04 14:42 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 .