Solved

how to separate the float number from string

Posted on 2008-10-30
13
351 Views
Last Modified: 2010-04-05
Hello Guys,
Can you tell me how can I receive only a float number from the string. For example:
string -  252.676g or f466.422d
BR
Vaalar
0
Comment
Question by:Vaalar
[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
  • 7
  • 3
  • 3
13 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 400 total points
ID: 22840148
use the Val function

function ConvertFloat(aText: string): Double;
var S: string;
  I, aCode: Integer;
begin
  // First delete all non number characters
  I := 1;
  while (I < Length(aText)) and not (aText[I] in ['-','+',DecimalSeparator,ThousandSeparator,'0'..'9']) do
    Inc(I);
  // Convert text to float
  Val(Copy(Atext, I, Length(aText)), Result, aCode);
end;
0
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 100 total points
ID: 22840218
This assumes only one number in there
I use the tryTo function instead of the val

function TForm1.returnFloat(const s: string): double;
var
   i: integer;
   temp: string;
begin
   result := 0;
   temp := '';
   for i := 0 to length( s) do
      if s[i] in ['0'..'9', DecimalSeparator,ThousandSeparator, '+', '-'] then
         temp := temp + s[i];

   if temp <> '' then
      tryStrToFloat( temp, result)
end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 22840268
Geert_Gruwez, your code doesnt take into account if there is a non numerical character at the end of the string.
0
Independent Software Vendors: 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!

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840278
no need the Val does this
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840294
the aCode will show the location of the problem

maybe Vaalar can evaluate further what he wants to happen ?
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 22840334
In fact it might be better to make sure you never continue past the first number that is found

function TForm1.returnFloat(const s: string): double;
var
   i, startPos, endPos: integer;
begin
   result := 0;

   startPos := 1;
   while ( not ( s[ startPos] in ['0'..'9', DecimalSeparator,ThousandSeparator, '+', '-'])) and
         ( startPos <= length( s)) do
      inc( startPos);

   endPos := startPos;
   while ( s[ endPos] in ['0'..'9', DecimalSeparator,ThousandSeparator, '+', '-']) and
         ( endPos <= length( s)) do
      inc( endPos);

   if endPos >= startPos then
      tryStrToFloat( copy( s, startPos, endPos - startPos), result)
end;
0
 

Author Comment

by:Vaalar
ID: 22840744
Thx for your help.
I have tested your ideas and it solves  but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
0
 

Author Comment

by:Vaalar
ID: 22840760
Thx for your help.
I have tested your ideas and it solves some problems  but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840976
you could first strip all non numerals:

function ConvertFloat(aText: string): Double;
var S: string;
  I, aCode: Integer;
begin
  // First delete all non number characters
  I := 1;
  s := '';
  while (I <= Length(aText)) and (aText[I] in ['-','+',DecimalSeparator,ThousandSeparator,'0'..'9']) do
  begin
    s := s + aText[I];
    Inc(I);
  end;
  // Convert text to float
  Val(S, Result, aCode);
end;
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22841015
this will never solve all problems
it will get the first number possible

but to resolve this you'll need to specify some rules:

aText := 'abc123.def456-789+1023xxx';

Value to string = '123.456-789+1023';
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22841022
woops slight mistake

function ConvertFloat(aText: string): Double;
var S: string;
  I, aCode: Integer;
begin
  // First delete all non number characters
  I := 1;
  s := '';
  while (I <= Length(aText)) do
  begin
    if (aText[I] in ['-','+',DecimalSeparator,ThousandSeparator,'0'..'9']) then
      s := s + aText[I];
    Inc(I);
  end;
  // Convert text to float
  Val(S, Result, aCode);
end;
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22841052
or try StrToFloatDef

Result := StrToFloatDef(S, 0);
0
 

Author Comment

by:Vaalar
ID: 22841236
Thank you very much Geert,
and thx also to Mike
BR
Vaalar
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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