Link to home
Start Free TrialLog in
Avatar of n1875621
n1875621

asked on

reallllllyyyy easy

why does this not give me 54.32, doesnt perl allow for floats?

$num1 = "22.00";
$num2 = "32.32";
$out = ($num1 + $num2);
print "the answer is $out";


Avatar of b2pi
b2pi
Flag of United States of America image

It does.  I'll bet you're running this from the command line, and you don't see any output at all. Try changing the last line to

print "the answer is $out\n";

(Basically, the prompt is overwriting your output).  

By the way, you don't need most of the quotes you put in.

$num1 = 22.00;
$num2 = 32.32;
$out = $num1 + $num2;
print "The answer is $out\n";

Will do what you want
Avatar of n1875621
n1875621

ASKER

No, actually i was using it across the cgi. and the quotes were there because the values are "strings", want i am wanting to know is how to explicitly cast them as floats (doubles, reals, whatever). I assume the anwser will look something like:

$out = string_to_float($num1) + string_to_float($num2);

im new to perl, i dont know how to cast variables, i would have thought that it had it inbuilt, but it doesnt seem to.

thanks,
john

Avatar of ozo
#or do you have

use integer;

#in your code?
Why do you think that
  $out = ($num1 + $num2);
or
  $out = $num1 + $num2;
doesn't seem to?
What answer are you getting?
did you put an html header in your code, i.e.

print "content:text/html\n";

and then the rest?

SLUT!!!! I was using an associative array, I spelled the index string incorrectly, it works fine now, sorry about that folks....

if anyone still wants the points, can to tell me a way to enforce 2 decimal places in a number, for example, if i have
"21.2", it converts it to "21.20" and if it is 32.12, it does nothing?

this should be easy, so ill leave the points as is.

thanks, sorry about the confusion

$num = sprintf "%.2f",21.2;
thanks dude, answer the question and ill happily hand out points.

perl is heaps like c eh? i wasnt aware that the (s)printf family was in perl.

thanks again
jkohn

like C, and Sed, and Awk, and Shell, and Lisp, and Basic, and English, and Greek, ...
for other inbuilt functions, see
perldoc perlfunc
arent u gonna answer it?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for ur help guys, sorry about the confusion earlier