Solved

check string for numbers

Posted on 2004-09-28
10
300 Views
Last Modified: 2010-04-05
I have a written a DLL which returns strings and integers from string. I am having a problem whereby if the string does not contain a valid number the real type conversion fails. What I need is a if else statement that checks there is a valid number.

Function lenofcall(S1,S2 : String) :Real;

Begin
Result := (StrToFloat(copy(S1,1,2))*60)+(StrToFloat(copy(S1,4,2)));
end

 for instance the following text would give a result of 129
S1 := '01:59'

but if
S1 := '----' or any other non number text,
then I want the result to give a result of '0'

any Ideas


0
Comment
Question by:lloydie-t
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12169284
just put it in a try except...

Function lenofcall(S1,S2 : String) :Real;
Begin
   try
      Result := (StrToFloat(copy(S1,1,2))*60)+(StrToFloat(copy(S1,4,2)));
   except
      Result := 0;
   end;
end;
0
 
LVL 1

Expert Comment

by:Balshe
ID: 12170263
HI
Try this

Function String_TO_REAL(STR:STRING):Real;
Const Number_Str='0123456789.';
Var
  Alpha:String;
  i:Integer;
Begin
  for i :=0 to length(Str) do
    Begin
      if pos(str[i],Number_Str)>0 then
       Alpha:=Alpha+str[i];
    end;
  Result:=StrToFloat(Alpha);
end;
0
 
LVL 1

Expert Comment

by:Balshe
ID: 12170285

Function String_TO_REAL(STR:STRING):Real;
Const Number_Str='0123456789.';
Var
  Alpha:String;
  i:Integer;
Begin
  for i :=1 to length(Str) do               // start from 1 not from 0
    Begin
      if pos(str[i],Number_Str)>0 then
       Alpha:=Alpha+str[i];
    end;
  Result:=StrToFloat(Alpha);
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12170844
For those who haven't noticed this: lloydie-t wants to convert a string containing a time to a number of minutes. So:

var
  Hour, Min, Sec, MSec: Word;
begin
  DecodeTime( StrToTime( '01:59' ), Hour, Min, Sec, MSec );
  Result := Hour * 60 + Min;
end;

And the result is 119, not 129... ;-)
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 125 total points
ID: 12170858
Oh, and since we want to result 0 if we don't have a valid time:

var
  Hour, Min, Sec, MSec: Word;
begin
  try
    DecodeTime( StrToTime( '01:59' ), Hour, Min, Sec, MSec );
    Result := Hour * 60 + Min;
  except Result := 0;
  end;
end;

Life isn't difficult if you think logical.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lloydie-t
ID: 12173933
Nearly there Alex, I have tried your code, but it does raise an excption when the string is not in a time format
FI: |SBNode is not a valid time.

this is the code I'm using

Function LenOfCall (S1,S2: String): Real; stdcall;
var
Hour, Min, Sec, Msec:Word;
Begin
        try
        DecodeTime(strToTime(copy(S1,50,8)),Hour,Min,Sec,Msec);
        Result := Hour*60+Min+Sec/60;
        except
        Result := 0;
        end;
end;

but it is much neater. Any other ideas


0
 

Author Comment

by:lloydie-t
ID: 12174382
Seems as though the debugger is working overtime. The exception is raised only when running the app in delphi, running it stand alone does'nt seem to cause a problem.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12175014
That might be related to the locale settings, the date-time separator. :-) But you could just use: S1[3] := ':';
And then use my solution to convert it.

Oh, and why: strToTime(copy(S1,50,8)) ???
You're copying 8 characters but the time would just be 5 characters. Maybe use: strToTime(copy(S1,50,5))
0
 

Author Comment

by:lloydie-t
ID: 12175148
The exception only happens when I receive a string such as '----------' or '|SBNode'. I read somewhere that the debugger can sometimes pick these up even when using 'try except'. Otherwise It seems to work OK and as expected, when not running in delphi using the same data.

Thanks fro your help.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12177759
Ehm, yes. The debugger picks up exceptions if you've set it up like that in de debug options under the Tools menu. Then the debugger stops at the exception, but if you press F9 it will just let the application handle it further. You'd have to turn that "Break on exception" option off if you don't want that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Activex loadlibrary and show parented form issue 6 285
Delphi TcxGrid group footer summary 3 203
Unique identifier on a terminal server (rdp) 4 45
Base1 Encode/Decode 3 67
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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