Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-11

Verify if string is a valid number

7 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho

Hello,

I would like to verify if a string is a valid number, as in StrToInt will work when applied to it. The obvious way is making a try statement:

      try
        StrToInt(TokenStr);
        AdicionarToken(tNumeroLiteral, TokenStr);
      except
        AdicionarToken(tId, TokenStr);
      end;

but I don't like this solution, because a lot of exceptions will be caused by normal execution of my program, which will disturb debugging it ...

It's easy to build such a routine, but I was just thinking that maybe there is one already somewhere in the RTL or FCL ... anyone know?

thanks,

Felipe Monteiro de Carvalho
Frank de Groot / Rent-a-Geek
to Felipe Monteiro de Carvalho

Have a Original Message -----

Earlier message 8 lines
From: "Felipe Monteiro de Carvalho" To: "FPC-Pascal users discussions" Sent: Sunday, November 23, 2008 5:18 PM Subject: [fpc-pascal] Verify if string is a valid number > It's easy to build such a routine, but I was just thinking that maybe > there is one already somewhere in the RTL or FCL ... anyone know?
Johann Glaser
to Felipe Monteiro de Carvalho: I would like to verify if a string is a valid number, as in StrToInt will work when…

Hi!

Am Sonntag, den 23.11.2008, 14:18 -0200 schrieb Felipe Monteiro de Carvalho:

Show quoted text 16 lines
Hello, I would like to verify if a string is a valid number, as in StrToInt will work when applied to it. The obvious way is making a try statement: try StrToInt(TokenStr); AdicionarToken(tNumeroLiteral, TokenStr); except AdicionarToken(tId, TokenStr); end; but I don't like this solution, because a lot of exceptions will be caused by normal execution of my program, which will disturb debugging it ...

Always avoid exceptions if possible, since they are very costly in terms CPU time. Do your error handling with exceptions, but not routine tests. This would be misuse of exceptions.

For your problem just use the original Pascal procedure Val(St:String;Var Value,Position:Integer)
which simply returns the position of an illegal character or 0 if the string was a legal number.

Bye
Hansi

Michael Van Canneyt
to Felipe Monteiro de Carvalho: I would like to verify if a string is a valid number, as in StrToInt will work when…
Felipe Monteiro de Carvalho wrote 19 lines
Hello, I would like to verify if a string is a valid number, as in StrToInt will work when applied to it. The obvious way is making a try statement: try StrToInt(TokenStr); AdicionarToken(tNumeroLiteral, TokenStr); except AdicionarToken(tId, TokenStr); end; but I don't like this solution, because a lot of exceptions will be caused by normal execution of my program, which will disturb debugging it ... It's easy to build such a routine, but I was just thinking that maybe there is one already somewhere in the RTL or FCL ... anyone know?
If TryStrToInt(s,i) then
  Writeln('S has a valid number:',I);

Or

  Val(S,I,C);
  If C=0 then
   Writeln('S has a valid number:',I);

Michael.

Graeme Geldenhuys
to Johann Glaser: Hi! Am Sonntag, den 23.11.2008, 14:18 -0200 schrieb Felipe Monteiro de Carvalho: Always…
Johann Glaser wrote
Always avoid exceptions if possible, since they are very costly in terms CPU time.

I know this was the rule of thumb in Delphi 5-7 days... Does this still apply today, with newer compilers and much faster CPU's? Just curious...

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
On Sun, Nov 23, 2008 at 6:23 PM, Johann Glaser wrote: > Always avoid exceptions if possible, since they are very costly in terms > CPU time. I know this was the rule of thumb in Delphi 5-7 days... Does this still apply today, with newer compilers and much faster CPU's? Just curious...

Yes. The code must save all registers, set a jump point etc. Quite costly.

Michael.