Testing if a String is a number

ncellier at ifrance.com ncellier at ifrance.com
Wed Jul 5 07:56:28 UTC 2006


Hi Damien

I suggest you use my Squeak NumberParser (See http://bugs.impara.de/view.php?id=3512 ) which has error handling a little bit more developped than just answering zero...

You could simply test is aNumber is read without error from (self readStream), and assert the stream atEnd.

Nicolas

Damien Cassou:
> Hi,
> 
> I'm surprised there is no #isNumber selector for strings. If I want to 
> detect if a string is a number or not, it's not so easy. One of the 
> possible implementation would be:
> 
> String>>isNumber
>    value := self asNumber.
>    value isZero
>         ifFalse: [^ true]
>         ifTrue:  [^ self first = $0]
> 
> 
> But this is buggy and incomplete:
> 
> 'dfdfd' isNumber false     <- correct
> '12' isNumber  true        <- correct
> '0' isNumber true          <- correct
> '0ef' isNumber true        <- Wrong !
> '2r0' isNumber false       <- Wrong !
> 
> 
> Should I open a bug report on Mantis ?
> 
> 


________________________________________________________________________
iFRANCE, exprimez-vous !
http://web.ifrance.com


More information about the Squeak-dev mailing list