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