To convert a string to a float in Perl, you can use the 'sprintf()' function.
In this example,
$str1
with a value of "3.14"
.sprintf()
function to convert the string to a float and store the result in a variable $float1
.$float1
.# Convert "3.14" String to Float
my $str1 = "3.14";
my $float1 = sprintf("%.2f", $str1);
print "Float value is: $float1\n";
Float value is: 3.14
In this example,
$str2
with a value of "10.5"
.sprintf()
function to convert the string to a float and store the result in a variable $float2
.$float2
.# Convert "10.5" String to Float
my $str2 = "10.5";
my $float2 = sprintf("%.2f", $str2);
print "Float value is: $float2\n";
Float value is: 10.50
In this tutorial, we learned How to convert String to Float in Perl language with well detailed examples.