Board index » delphi » TStringField and calculating problem

TStringField and calculating problem

Hello!

I have Paradox table and TStringField. Trying to calculate its value: first
converting
string to double and making calculatons then convert result back to String,
but i get wrong results:
When value grows to 20 or more i get 24,03 or 24,01.

Is the problem in StrToFloat?.
I must use StringField, because user wants to type here string or Float
value.

Another question: I have FloatField in same table and making same
calculations like before
and wondering how to keep zeros after number. Example:
Program works like this:
1,25
1,5
1,75
2

Must be:
1,25
1,50
1,75
2,00

Code from DBEdit KeyDown event:

Max := 30.00;
Min := -30.00;
inc := 0.25;

  case key of
  VK_UP   : begin
              try
                dbl := StrToFloat((fld as TStringField).Value);
              except
              on EConvertError do
                dbl := 0;
              end;
                if dbl >= Max then dbl := Min;
                dbl := dbl + inc;
                (fld as TStringField).Value := FloatToStr(dbl);
            end;
  VK_DOWN : begin
              try
                dbl := StrToFloat((fld as TStringField).Value);
              except
              on EConvertError do
                dbl := 0;
              end;
                if dbl <= Min then dbl := Max;
                dbl := dbl + inc;
               (fld as TStringField).Value := FloatToStr(dbl);
            end;

Thanks!
Tomi Hakala

 

Re:TStringField and calculating problem


Quote
Tomi Hakala wrote:

> Another question: I have FloatField in same table and making same
> calculations like before
> and wondering how to keep zeros after number. Example:
> Program works like this:
> 1,25
> 1,5
> 1,75
> 2

Let's say have Amount field in your Table, then use the Amount
Field's Display Format property. To get both two decimals AND thousand
separators to your numbers, put there something like ",0.00" or
".0,00", depending of your Windows decimal separator settings.

Quote
> I must use StringField, because user wants to type here string or Float
> value.

I don't quite get this, do you mean your users can choose to type
there both words, or decimal numbers. And you should then sort some
meaningful number out of it?  
What is the Field type for this kind of data in your Paradox table?

Markku Nevalainen

Re:TStringField and calculating problem


Quote
"Markku Nevalainen" <m...@iki.fi> wrote in message

news:3A8F0C5A.67F6@iki.fi...

Quote
> Tomi Hakala wrote:

> > Another question: I have FloatField in same table and making same
> > calculations like before
> > and wondering how to keep zeros after number. Example:
> > Program works like this:
> > 1,25
> > 1,5
> > 1,75
> > 2

> Let's say have Amount field in your Table, then use the Amount
> Field's Display Format property. To get both two decimals AND thousand
> separators to your numbers, put there something like ",0.00" or
> ".0,00", depending of your Windows decimal separator settings.

> > I must use StringField, because user wants to type here string or Float
> > value.

> I don't quite get this, do you mean your users can choose to type
> there both words, or decimal numbers. And you should then sort some
> meaningful number out of it?
> What is the Field type for this kind of data in your Paradox table?

> Markku Nevalainen

Thanks!. DisplayFormat property works fine.

Field type is string, because user can type string or float value in this
field.
When user is editing field i check if value is float then inc or dec value
depending
which key is pressed (VK_UP or VK_DOWN). If value is string, user no need to
change value like i say.

Re:TStringField and calculating problem


Quote
Tomi Hakala wrote:

> When user is editing field i check if value is float then inc or dec value
> depending

Try using Format instead of FloatToStr to turn Float value to String
  String1 := Format('%n',[MyFloatValue]);

By default, the '%n' parameter will always return the string with
2 decimals.

Markku Nevalainen

Other Threads