Solved

Float Conversion

Posted on 1997-05-09
6
275 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
  • 3
  • 3
6 Comments
 
LVL 2

Accepted Solution

by:
javiertb earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

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

Author Comment

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

Expert Comment

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now