Reputation: 2141
Below is my decimal variable:
my $variable = 1.0;
When I try to print the variable,
print "$variable\n";
I got 1.0.
But, if I pass the same to a function:
&printvariable("$variable\n");
sub printvariable()
{
print "My variable is: @_";
}
Now, it is printing as, My variable is: 1
Why is this decimal value getting rounded off in the function? Is there a way to print the value as it is in function also?
Upvotes: 0
Views: 823
Reputation: 385556
What you said isn't true.
$ perl -e'my $variable=1.0; print "$variable\n";'
1
$variable=1.0;
, $variable=1;
and $variable= 4-3;
all assign the number one to $variable
, and interpolation stringifies one to 1
. It has no way to know the code that produced the value was 1.0
or 4-3
, nor should that be important.
If you want to display a decimal point, either assign the three-character string 1.0
to $variable
$ perl -e'my $variable = "1.0"; print "$variable\n";'
1.0
Or convert the number to that string.
$ perl -e'my $variable = 1.0; printf "%.1f\n", $variable;'
1.0
Upvotes: 5