antony burden
asked on
Float Conversion
procedure TForm1.txtPressureChange(S ender: TObject);
var
Pressure: Single;
begin
if length(txtPressure.Text) > 0 then
begin
Label1.Caption := FormatFloat('0.00', StrToFloat(txtPressure.Tex t));
Pressure := StrToFloat(Label1.Caption)
end;
Label2.Caption := FloatToStr(Pressure)
end;
This is my attempt to ensure that the variable Pressure is the same as entered in txtPressure,
however the results I get are as follows:
txtPressure.Text '10.1'
Label1.Caption '10.10'
Label2.Caption '10.1000003814697'
so Pressure = 10.1000003814697 not 10.1, how can I fix this please.
Antony
var
Pressure: Single;
begin
if length(txtPressure.Text) > 0 then
begin
Label1.Caption := FormatFloat('0.00', StrToFloat(txtPressure.Tex
Pressure := StrToFloat(Label1.Caption)
end;
Label2.Caption := FloatToStr(Pressure)
end;
This is my attempt to ensure that the variable Pressure is the same as entered in txtPressure,
however the results I get are as follows:
txtPressure.Text '10.1'
Label1.Caption '10.10'
Label2.Caption '10.1000003814697'
so Pressure = 10.1000003814697 not 10.1, how can I fix this please.
Antony
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this:
var
idecimals,idigits: integer;
Pressure: single;
begin
idecimals:=pos(',',txtPres sure.Text) ; // decimals separator
if (idecimals>0) then
idigits:=length(txtPressur e.Text)-id ecimals
else
idigits:=0;
Pressure:=strtofloat(txtPr essure.tex t);
Label1.Caption:= FloattoStrF(txtPressure.Te xt,ffFixed ,7,idigits );
end;
Label1 should be the same as txtPressure.
var
idecimals,idigits: integer;
Pressure: single;
begin
idecimals:=pos(',',txtPres
if (idecimals>0) then
idigits:=length(txtPressur
else
idigits:=0;
Pressure:=strtofloat(txtPr
Label1.Caption:= FloattoStrF(txtPressure.Te
end;
Label1 should be the same as txtPressure.
ASKER
Thankyou, I'll try this very soon.Thanks a lot.Antony
ASKER
Could you explain why the number displayed is so wierd?
No idea. It isn't logical. If you introduce '10,10' why you get 10,1000543 or whatever instead of 10,1000000 (what would be the logical result)?? Better to ask Borland for this.
ASKER