Solved

reallllllyyyy easy

Posted on 1998-10-20
12
152 Views
Last Modified: 2010-03-05
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";


0
Comment
Question by:n1875621
  • 5
  • 5
  • 2
12 Comments
 
LVL 5

Expert Comment

by:b2pi
ID: 1205557
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
0
 

Author Comment

by:n1875621
ID: 1205558
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 1205559
#or do you have

use integer;

#in your code?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 1205560
Why do you think that
  $out = ($num1 + $num2);
or
  $out = $num1 + $num2;
doesn't seem to?
What answer are you getting?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1205561
did you put an html header in your code, i.e.

print "content:text/html\n";

and then the rest?

0
 

Author Comment

by:n1875621
ID: 1205562
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 1205563
$num = sprintf "%.2f",21.2;
0
 

Author Comment

by:n1875621
ID: 1205564
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 1205565
like C, and Sed, and Awk, and Shell, and Lisp, and Basic, and English, and Greek, ...
for other inbuilt functions, see
perldoc perlfunc
0
 

Author Comment

by:n1875621
ID: 1205566
arent u gonna answer it?
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 1205567
printf "%.2f","22.00"+"32.32";
0
 

Author Comment

by:n1875621
ID: 1205568
thanks for ur help guys, sorry about the confusion earlier
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question