Solved

reallllllyyyy easy

Posted on 1998-10-20
12
149 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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.…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 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

12 Experts available now in Live!

Get 1:1 Help Now