How to format DBTextEdit (float field) as a percent?

I have a data entry form that has a DBTextEdit field pulling in a float but when I show the form, it needs to show as a percentage. I have this same field in a grid and the procedure I have is the following:

if (AText <> NULL) OR (AText <> '') then begin
    AText := FloatToStr(StrToFloatDef(AText,0) * 100);
    AText := AText + '%';
  end;

I tried this in both the OnValidate and OnNewLookupDisplayText and those did not work... any ideas how to accomplish this? I'm sure it will be simple and I am just overlooking something really silly... thanks for your help!
Norm-alNetwork EngineerAsked:
Who is Participating?
 
Mahdi78Connect With a Mentor Commented:
This is my own method :)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure MyProcedure(Sender: TField; var Text: string;
      DisplayText: Boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyProcedure(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
if (Sender.AsString <> NULL) OR (Sender.AsString <> '') then
    Text := FloatToStr(Sender.AsFloat * 100) + '%';
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
(Table1.FieldByName('MyField') as TField).OnGetText := MyProcedure;
end;

end.

Open in new window

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
You should use field.ongettext

if (AText <> NULL) OR (AText <> '') then begin
    AText := FormatFloat('#,### %',(StrToFloatDef(AText,0) * 100));

0
 
Norm-alNetwork EngineerAuthor Commented:
I dont have an event for OnGetText for the DBTextEdit, I only have that in the grid.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
I was meaning in the Table.field.ongettext
0
 
Norm-alNetwork EngineerAuthor Commented:
Oh ok... I already have that procedure for the field in the table but I need to also format to percent for the field that is not in the table, it is just an edit box on a form.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
What is the class of the component?
0
 
Norm-alNetwork EngineerAuthor Commented:
Worked great, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.