Board index » delphi » Question: TFloatField displayed as Percentage

Question: TFloatField displayed as Percentage

Anyone know of a simple way to display a TFloatField as a precentage?  
E.g. if the value were 0.085, I'd like it displayed as 8.5%.  I've been
digging around in the DisplayFormat of TFloatField, but haven't found
gold yet. Thanks.

Jim Butler

 

Re:Question: TFloatField displayed as Percentage


Quote
Jim Butler wrote:

> Anyone know of a simple way to display a TFloatField as a precentage?
> E.g. if the value were 0.085, I'd like it displayed as 8.5%.  I've been
> digging around in the DisplayFormat of TFloatField, but haven't found
> gold yet. Thanks.

Try using the field's OnGetText Event to return the formatted value with  
the formatfloat function.

Example:

procedure MyForm.TheFieldGetText(Sender:Tfield;var
Text:string;DisplayText:Boolean);

Begin
        Text:=FormatFloat('#0.0"%"',TheField.Value * 100);
End;

NOTE: If you are also letting the user edit the field, you will need to
define the reverse conversion in the OnSetText Event.

NOTE: To add the event handler to the field, open the field editor and
press F11 after selecting TheField.  This will pop up the object
inspector for the selected field.  The OnSetText event is on the field's
events page.

Hope this helps,

Alain Toutant
a.tout...@sympatico.ca

Other Threads