Solved

reallllllyyyy easy

Posted on 1998-10-20
12
146 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now