Using FormatFloat in FastReport VCL 5 scripts

Caynadian picture Caynadian · Jan 15, 2015 · Viewed 7.8k times · Source

I am trying to print a currency formatted value to a TfrxMemoView on the report with the following script:

procedure txCreditLimitOnBeforePrint(Sender: TfrxComponent);
begin
  if <TRAN."CREDITAPPROVED"> = 1 then                                
    txCreditLimit.Text := 'Credit Limit: ' + FormatFloat('%2.2m', <TRAN."CREDITLIMIT">)
  else
    txCreditLimit.Text := '';
end;

But all that I get out is %2.2m instead of the actual value. What am I doing wrong?

Answer

bummi picture bummi · Jan 16, 2015

The function FormatFloat in FastReport is working like FormatFloat in Delphi, so you might use:

procedure txCreditLimitOnBeforePrint(Sender: TfrxComponent);
begin
  if <TRAN."CREDITAPPROVED"> = 1 then                                
    txCreditLimit.Text := 'Credit Limit: ' + FormatFloat('#,##0.00 €', <TRAN."CREDITLIMIT">)
  else
    txCreditLimit.Text := '';
end;

Since FormatFloat does not support system currency another way could be to use a memo bound to your dataset containing the expression like e.g. Credit Limit: [TRAN."CREDITLIMIT"] and format this memo in the Object Inspector, with the syntax you mentioned. You condition for printing would change to:

procedure txCreditLimitOnBeforePrint(Sender: TfrxComponent);
begin
  txCreditLimit.Visible := <TRAN."CREDITAPPROVED"> = 1;
end 

enter image description here

Expressions enclosed in square brackets will be calculated in TextObjects, so for example
[<DS."a">] * 2 := [<DS."a"> + <DS."a">] will lead to an output of: 12.50 € * 2 = 25.00 €
if the format of the TfrxMemoView is defined as %2.2m. In the shown example both terms (included in square brackets) are formatted, the second one is in addition calculated.