Board index » delphi » Restrict display to 2 decimal places

Restrict display to 2 decimal places

Hello all,

How does one restrict the display in a DBGrid of a TFloatField to only 2
decimals, specifically at run-time?

After my user selects an ACCESS 97 mdb file, I loop through the Fielddefs as
follows:

for i := 0 to table1.fielddefs.count-1 do
 with table1.fielddefs[i] do
  if FieldClass = TFloatField then Precision := 2;

However, the display still shows all of the decimals places (up to 15?) and
does not round to the selected precision. Am I off-track here?
Any Suggestions?

Thanks in advance,
Regards,
Bill
myname := 'bill_richards';
myISP := 'nic.edu';
myemail := myname + '@' + myISP;

 

Re:Restrict display to 2 decimal places


I haven't used anything but Paradox files
so I may be way off base here, but have you
tried setting the DisplayFormat of the Tfield.
The Tfields show up when you double click on the
Table component and right click on the list and choose
add fields.

Bill d. Richards <bi...@icehouse.net> wrote in message
news:7mangf$p2o10@forums.borland.com...

Quote
> Hello all,

> How does one restrict the display in a DBGrid of a TFloatField to only 2
> decimals, specifically at run-time?

> After my user selects an ACCESS 97 mdb file, I loop through the Fielddefs
as
> follows:

> for i := 0 to table1.fielddefs.count-1 do
>  with table1.fielddefs[i] do
>   if FieldClass = TFloatField then Precision := 2;

> However, the display still shows all of the decimals places (up to 15?)
and
> does not round to the selected precision. Am I off-track here?
> Any Suggestions?

> Thanks in advance,
> Regards,
> Bill
> myname := 'bill_richards';
> myISP := 'nic.edu';
> myemail := myname + '@' + myISP;

Re:Restrict display to 2 decimal places


Set the DisplayFormat property of the TFloatField field object to #.00

Bill

--

Bill Todd - TeamB
(TeamB cannot respond to email questions. To contact me
 for any other reason remove nospam from my address.)

Re:Restrict display to 2 decimal places


Art,

Quote
Art Begun wrote in message <7mb2sd$p2...@forums.borland.com>...
>I haven't used anything but Paradox files
>so I may be way off base here, but have you
>tried setting the DisplayFormat of the Tfield.
>The Tfields show up when you double click on the
>Table component and right click on the list and choose
>add fields.

Thanks for your suggestion. However, in my original post, I specified that I
needed to set thngs like displayformat at runtime, not design time by the
method you suggested. This would work for persistent fields, as opposed to
dynamic fields. In my application, I can not set all the fields persistent
because I don't know what they might be (and you can't mix persistent and
dynamic fields). However, working from your suggestion I was able (after
nearly TWO hours with the online docs and trial and error) to figure out how
to set the displayformat property at runtime for dynamic field components.
Apparently, the DisplayFormat property isn't "published" at runtime and is
only intended to be used from FieldComponent editor at designtime. Therefore
the following causes a compile-time error of 'Undeclared identifier :
displayformat' :

table1.FieldByName('Field1').displayformat := '0.##';

But, using a trick that someone more experienced than I showed me, I tried
putting the following in the beginning of the Type section:

tTableCracker = Class(TTable);

and then the following works:

tTableCracker(table1.FieldByName('Field1')).displayformat := '0.##';

Regards,
Bill
myname := 'bill_richards';
myISP := 'nic.edu';
myemail := myname + '@' + myISP;

Re:Restrict display to 2 decimal places


Bill,

Quote
Bill Todd wrote in message <7mbjdc$p...@forums.borland.com>...
>Set the DisplayFormat property of the TFloatField field object to #.00

Actually, it should be set to '0.##'. Also, setting the DisplayFormat
property of dynamic field components is not all that straightforward, and is
NOT documented. See my previous response for my solution.

Regards,
Bill
myname := 'bill_richards';
myISP := 'nic.edu';
myemail := myname + '@' + myISP;

Re:Restrict display to 2 decimal places


On Sun, 11 Jul 1999 21:24:10 -0700, "Bill d. Richards"

Quote
<bi...@icehouse.net> wrote:
>Apparently, the DisplayFormat property isn't "published" at runtime and is
>only intended to be used from FieldComponent editor at designtime. Therefore
>the following causes a compile-time error of 'Undeclared identifier :
>displayformat' :

>table1.FieldByName('Field1').displayformat := '0.##';

DisplayFormat is defined for TNumericField and descendants (like
TFloatField).  FieldByName returns a TField (even if the actual
fieldtype is TFloatField).  So you have to typecast:

Field := Table1.FieldByName('Field1');
if Field is TNumericField then
  TNumericField(Field).DisplayFormat := ...

Jan

Re:Restrict display to 2 decimal places


Jan,

Quote
Jan Sprengers wrote in message <3789993f.407393...@forums.inprise.com>...
>DisplayFormat is defined for TNumericField and descendants (like
>TFloatField).  FieldByName returns a TField (even if the actual
>fieldtype is TFloatField).  So you have to typecast:
>. . .

Thanks for clearing up the specifics for me.
Regards,
Bill

Other Threads