I don't know C# well enough to know if there's an equivalent way to do
this (I just started following this newsgroup while at the same time
learning Java) but in Smalltalk I would add the following method to
CharacterArray (superclass of String):
isNumeric
"
return true if the string is a valid numeric expression:
'123' isNumeric - true
'123.45' isNumeric - true
'.45' isNumeric - true
'+123.45' isNumeric - true
'-123.45' isNumeric - true
'12.3.45' isNumeric - false
'123.45e100' isNumeric - true
'123.45e-100' isNumeric - true
'5.23e-6' isNumeric - true
'5.23e-6e' isNumeric - false
we use the current locale's definition of the decimal point character.
"
^self matchesRegex: (('[+-]?[0-9]*\' copyWith: Locale current
numberPolicy decimalPoint) , '?[0-9]+(e-?[0-9]+)?')
To avoid the whole "if it's a null" thing, I would add an isNumeric to
Object that simply returns false--so it wouldn't matter if it was a
String, a nil object, or an object of any other type--except perhaps a
subclass of Number--which should probably return "true", after all,
numbers are numeric... ;-)