slicks1 slicks1 - 3 months ago 8
Perl Question

Cannot get correct return value in sprintf function call

My problem is that the return value im printing is not being translated into a value.

I have the following code in a test file.

#!/usr/bin/perl -I/srv/www/jonathan/m/www
my $var = sprintf("$%.1f lbs",(77*2.20462));
print $var;


Its returning:
0.1f
instead of the value i need to see.

What am i doing incorrectly here? I'm a perl newbie.

Answer

Perl uses dolar($) symbol to declare a scalar variable, you need to remove the $ symbol from the sprintf.

corrected code

my $var = sprintf("%.1f lbs",(77*2.20462));
print $var;

output

169.8 lbs
Comments