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";
$num1 = "22.00";
$num2 = "32.32";
$out = ($num1 + $num2);
print "the answer is $out";
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
$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
#or do you have
use integer;
#in your code?
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?
$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?
print "content:text/html\n";
and then the rest?
ASKER
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
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;
ASKER
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
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
for other inbuilt functions, see
perldoc perlfunc
ASKER
arent u gonna answer it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for ur help guys, sorry about the confusion earlier
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