Recent

Author Topic: ' ' is not a valid float  (Read 2353 times)

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
' ' is not a valid float
« on: February 18, 2017, 09:30:47 am »
im currently working on a program that when a certain number is inserted in the first TEdit and a calculate button is clicked the second TEdit displays the total and the third TEdit displays the mean of the total.
but when i run the program and click on the calculate button i get a " ' ' is not a valid float" error.
i've gone through the code and even had a look at the string grid example but i still get the error.
what could be the cause of this?

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: ' ' is not a valid float
« Reply #1 on: February 18, 2017, 09:43:02 am »
An edit control is always a string representation in the case of a float.
You need to convert it to a real float before you can use it in calculations.
The best way to do that is with TryStrToFloat see: http://www.freepascal.org/docs-html/rtl/sysutils/trystrtofloat.html

From the release of 3.0.2 yesterday you can also do this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    s:single;  //or double, whatever
  4. begin
  5.   // no helpers for UTF8 yet, but AnsiString.
  6.   s:=AnsiString(edit1.Text).ToSingle;  // or ToDouble...
  7.   caption := FloatToStr(s);  // do something, so we know it works
  8. end;
     
« Last Edit: February 18, 2017, 10:03:13 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018