Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

reallllllyyyy easy

Posted on 1998-10-20
12
Medium Priority
?
162 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 85

Expert Comment

by:ozo
ID: 1205559
#or do you have

use integer;

#in your code?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

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 85

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 85

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 85

Accepted Solution

by:
ozo earned 80 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

581 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