fixxxer fixxxer - 1 month ago 6x
Perl Question

How can I convert a string to a float with Perl?

Is there any function like

which can convert a string to float value?
I'm currently using the following code:


I need to convert the string returned by
to float.


Just use it. In Perl, a string that looks like a number IS a number.

Now, if you want to be sure that the thing is a number before using it then there's a utility method in Scalar::Util that does it:

use Scalar::Util qw/looks_like_number/;


if (looks_like_number($input)) {
    $input += 1; # use it as a number!

Based on the sample input you left in the comments, a more robust method of extracting the number is:

$line =~ /([^\[\],]+)/; # <-- match anything not square brackets or commas
$input = $1;            # <-- extract match