How can we convert a string to a number in Perl?
In Perl, you can use the int function to convert a string into an integer. For example:
my $str = "123";
my $num = int($str);
print $num; # 输出 123
To convert a string into a floating point number, you can use a variant of the int function called sprintf. For example:
my $str = "3.14";
my $num = sprintf("%.2f", $str);
print $num; # 输出 3.14
Furthermore, if there are non-numeric characters in the string, the conversion result will be 0. If you want to throw an error or ignore the character when encountering a non-numeric character, you can use regular expressions for handling.