Testing if a String is a number

Damien Cassou damien.cassou at laposte.net
Wed Jul 5 07:37:57 UTC 2006


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 ?



More information about the Squeak-dev mailing list