# Float Conversion

procedure TForm1.txtPressureChange(Sender: TObject);
var
Pressure: Single;
begin
if length(txtPressure.Text) > 0 then
begin
Label1.Caption := FormatFloat('0.00', StrToFloat(txtPressure.Text));
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
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Instead of using FloatToStr use FloatToStrF where you can set Precission and Digits. Look up this function in delphi on-line help.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Label2 was only used to show the strange value, I need to ensure that variable Pressure is exactly the same as the value entered in txtPressure, I'm not worried about Label1 or Label2, just Pressure.Please can someone correctly answer this problem.Antony
0
Commented:
Try this:

var
idecimals,idigits: integer;
Pressure: single;
begin
idecimals:=pos(',',txtPressure.Text); // decimals separator
if (idecimals>0) then
idigits:=length(txtPressure.Text)-idecimals
else
idigits:=0;
Pressure:=strtofloat(txtPressure.text);
Label1.Caption:=                                  FloattoStrF(txtPressure.Text,ffFixed,7,idigits);
end;

Label1 should be the same as txtPressure.
0
Author Commented:
Thankyou, I'll try this very soon.Thanks a lot.Antony
0
Author Commented:
Could you explain why the number displayed is so wierd?
0
Commented:
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.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.