Solved

# Float Conversion

Posted on 1997-05-09
278 Views
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
0
Question by:antony burden
• 3
• 3

LVL 2

Accepted Solution

javiertb earned 50 total points
ID: 1336035
Instead of using FloatToStr use FloatToStrF where you can set Precission and Digits. Look up this function in delphi on-line help.
0

Author Comment

ID: 1336036
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

LVL 2

Expert Comment

ID: 1336037
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 Comment

ID: 1336038
Thankyou, I'll try this very soon.Thanks a lot.Antony
0

Author Comment

ID: 1336039
Could you explain why the number displayed is so wierd?
0

LVL 2

Expert Comment

ID: 1336040
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Virtuailstring tree compare node issue 14 125
Find and Replace Stream with 0s 8 69
I want to use librsync in my Delphi backup application. 3 58
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…