Solved

Float Conversion

Posted on 1997-05-09
6
283 Views
Last Modified: 2010-04-06
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
Comment
Question by:antony burden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 2

Accepted Solution

by:
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

by:antony burden
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

by:javiertb
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Author Comment

by:antony burden
ID: 1336039
Could you explain why the number displayed is so wierd?
0
 
LVL 2

Expert Comment

by:javiertb
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question