Jump to content
Andrew Spencer

Format a Float field text using another field value

Recommended Posts

I have a table with a float field, and would like to format the decimal places displayed for this value based on the content of another (integer) field in the same record.

 

Access is via a TFDQuery.

 

I tried to hook the OnGetText event of the float field in question with the following code, but this is giving an access violation error.

 

procedure TfrmMain.ItemValueFormat(Sender: TField; var Text: String; DisplayText: Boolean);
begin
  Text := Format('%.*f', [qMyTable.FieldByName('decimals').AsInteger, Sender.Value]);
end;

If anyone has tried something like this before, I'd appreciate some tips/code snippets. Thanks!

Share this post


Link to post

Answering my own questions...

 

The line should have been
 

  Text := Format('%.*f', [TfrmMain.qMyTable.FieldByName('decimals').AsInteger, Extended(Sender.Value)]);

 

Share this post


Link to post

I would not use any global variable at all.  The TField has access to the DataSet that owns the current record's fields, eg:

Text := Format('%.*f', [Sender.DatSet.FieldByName('decimals').AsInteger, Extended(Sender.Value)]);

Alternatively:

Text := FloatToStrF(Sender.Value, ffFixed, 18, Sender.DatSet.FieldByName('decimals').AsInteger);

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×