Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TFDQuery Field DisplayFormat


This question is answered.


Permlink Replies: 4 - Last Post: Sep 1, 2016 4:29 AM Last Post By: Tallys Ferrante
Tallys Ferrante

Posts: 14
Registered: 5/29/11
TFDQuery Field DisplayFormat  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2016 11:08 AM
In my DBGrid and components DBEdits my numeric fields with decimals ending in 0 are shown as (15,5 instead of 15,50 or 10 instead of 10,00). I used the IBO Components before FireDAC and my fields were already standard with two decimal places, where I had to format only the fields that had more than two decimal places. I'm using Firebird 2.5 btw.

I tried to make use of the option FmtDisplayFormat in FormatOptions of TFDQuery, it solves my problem, but my Integer fields also start to present decimal places.

I tried to fix my problem setting the format in a procedure that i created on the AfterOpen event from the TFDQuery, but it is very slow in queries with many numeric fields:

FormatDecimalPlaces procedure (FDQuery: TFDQuery);
var
Y, DecimalPlaces: Integer;
Mask, Name: String;
Begin
  is Y: = 0 to FDQuery.FieldCount - 1
  Begin
    if FDQuery.Fields [Y] .DataType in [TFieldType.ftCurrency, TFieldType.ftBCD, TFieldType.ftFMTBcd, TFieldType.ftExtended] then
    Begin
      Name: = FDQuery.Fields [Y] .FieldName;
      DecimalPlaces: = FDQuery.FieldByName (Name) .Size;
 
      Mask: = '0'. + LeftPad ( '', '0', DecimalPlaces);
      TNumericField (FDQuery.FieldByName (Name)) DisplayFormat: = Mask;.
    end;
  end;
end;


I want to know if there is an easier way to solve my problem

Thanks in advance!
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: TFDQuery Field DisplayFormat  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2016 6:36 AM   in response to: Tallys Ferrante in response to: Tallys Ferrante
Could you please describe your issue more precisely, including:
* what is DB field definition;
* sample value in this field;
* what you are getting in DBEdit;
* what you want to see in DBEdit.

--
With best regards,
Dmitry
Tallys Ferrante

Posts: 14
Registered: 5/29/11
Re: TFDQuery Field DisplayFormat  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 29, 2016 4:30 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Hello!

I have a numeric field on firebird( Numeric(15,2) )

Sample Value: 15,50

What i m getting in DBEdit: 15,5
What i want to see in DBEdit: 15,50

I Tried format my fields with:

TNumericField(Query.FieldByname('BUDGET')).DisplayFormat := ',0.00';


Works, but i have tables with 100+ numeric fields, getting too slow.

Dmitry Arefiev wrote:
Could you please describe your issue more precisely, including:
* what is DB field definition;
* sample value in this field;
* what you are getting in DBEdit;
* what you want to see in DBEdit.

--
With best regards,
Dmitry
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: TFDQuery Field DisplayFormat
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 31, 2016 10:52 PM   in response to: Tallys Ferrante in response to: Tallys Ferrante
Works, but i have tables with 100+ numeric fields, getting too slow.

You should surround setting of DisplayFormat by DisableControls / EnableControls and
eluminate FieldByName calls:
procedure FormatDecimalPlaces (FDQuery: TFDQuery);
var
Y, DecimalPlaces: Integer;
Mask: String;
Begin
  FDQuery.DisableControls;
  try
    for Y: = 0 to FDQuery.FieldCount - 1 do 
    Begin
      if FDQuery.Fields [Y] .DataType in [TFieldType.ftCurrency, TFieldType.ftBCD, TFieldType.ftFMTBcd, TFieldType.ftExtended] then
      Begin
        DecimalPlaces: = FDQuery.Fields [Y].Size;
        Mask: = '0'. + LeftPad ( '', '0', DecimalPlaces);
        TNumericField (FDQuery.Fields [Y]).DisplayFormat: = Mask;
      end;
    end;
  finaly
    FDQuery.EnableControls;
  end;
end;


--
With best regards,
Dmitry
Tallys Ferrante

Posts: 14
Registered: 5/29/11
Re: TFDQuery Field DisplayFormat  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2016 4:29 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Thanks Dmitry! Works perfectly!

Dmitry Arefiev wrote:
Works, but i have tables with 100+ numeric fields, getting too slow.

You should surround setting of DisplayFormat by DisableControls / EnableControls and
eluminate FieldByName calls:
procedure FormatDecimalPlaces (FDQuery: TFDQuery);
var
Y, DecimalPlaces: Integer;
Mask: String;
Begin
  FDQuery.DisableControls;
  try
    for Y: = 0 to FDQuery.FieldCount - 1 do 
    Begin
      if FDQuery.Fields [Y] .DataType in [TFieldType.ftCurrency, TFieldType.ftBCD, TFieldType.ftFMTBcd, TFieldType.ftExtended] then
      Begin
        DecimalPlaces: = FDQuery.Fields [Y].Size;
        Mask: = '0'. + LeftPad ( '', '0', DecimalPlaces);
        TNumericField (FDQuery.Fields [Y]).DisplayFormat: = Mask;
      end;
    end;
  finaly
    FDQuery.EnableControls;
  end;
end;


--
With best regards,
Dmitry
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02