truncate string to integer or decimal

From strings stored in variables, I need to isolate and use the first numerical value contained within them. I will need to know how to produce an integer as well as a floating point decimal. It needs to work on any string regardless of what types of characters (if any) are preceding or following the first numerical value contained in the string.

stringA="123.456:789 example string"
stringB="example string 123.456:789 example string"

Both strings should produce "123" when calling for an integer.
Both strings should produce "123.456" when calling for a floating point decimal.

I also would like to know how to replicate JavaScript's parseInt(), and parseFloat() functions. So stringA would produce a similar result, but stringB would result with NaN, because it doesn't begin with a numerical value. It would essentially be truncating the string to the end of the leading series of numerical characters, unless the leading character is non-numerical.

I would like to see several simple/efficient ways to accomplish these tasks. If anyone has ideas, please share.

Sorry, I'm just a JS developer, but I can't believe I haven't been able to find a good example. I've caked my question with keywords I was using, for anyone else feeling my pain. :wall:

---------- Post updated at 05:51 PM ---------- Previous update was at 05:38 PM ----------

One of my co-developers got back to me on part of my question. Here's a start. This doesn't strip off any leading non-numerical characters, but does emulate JavaScript parseInt and parseFloat.

stringA="123.456:789 example string"

newAFloat=`echo $stringA | egrep -o  '^[0-9]+(\.[0-9]+)?'`
echo $newAFloat
#123.456
newAInt=`echo $stringA | egrep -o  '^[0-9]+'`
echo $newAInt
#123

A few more with Perl:

$
$ stringA="123.456:789 example string"
$ stringB="example string 123.456:789 example string"
$ stringC="example string .123.456:789 example string"
$
$ # decimals
$ echo $stringA | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$ echo $stringB | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$ echo $stringC | perl -plne 's/^.*?(\d+).*?$/$1/'
123
$
$ # floats
$ echo $stringA | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
123.456
$ echo $stringB | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
123.456
$ echo $stringC | perl -plne 's/^.*?(\d*\.\d+|\d+\.\d+).*?$/$1/'
.123
$
$

tyler_durden

Awesome, awesome, awesome.

$ echo $str | sed 's,[aA-zZ],,g' | cut -d'.' -f1