CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating 0's after decimals

 



xcom
stranger

May 22, 2001, 12:45 AM

Post #1 of 15 (3581 views)
Creating 0's after decimals Can't Post

Okay. I have this:
$total=($count*.03);

and then
print "\$$total";

if $count is at say 50 or 500
it returns $1.5 or $15.
How do i get this to print $1.50 or $15.00?

Thanks




mhx
Enthusiast / Moderator

May 22, 2001, 2:03 AM

Post #2 of 15 (3580 views)
Re: Creating 0's after decimals [In reply to] Can't Post

Use the printf function. If you're familiar with C programming, it has (nearly) the same syntax. Try

Code
printf "\$%.2f", $total;

-- Marcus



xcom
stranger

May 22, 2001, 11:52 AM

Post #3 of 15 (3566 views)
Re: Creating 0's after decimals [In reply to] Can't Post

Hey thanks again Marcus!
I had a little trouble working it into my script because I had lines like this
print "<tr><td width=50%><B>Total Payout = <font color=FF0000><B>$total </B></font></td></tr>";
print "</table>";

I couldnt replace $total in <B></B> with that printf line,and I couldnt seem to give the printf command a seperate value like:
$total2 = (printf "\$%.2f" ,$total;);
So I just broke the table into seperate lines:
print "<tr>";
print "<td width=50%>";
print "<B>Total Clicks = </B>";
print "<font color=FF0000><B>$count</B></font>";
print "</td>";
print "<td width=50%>";
print "<B>";
print "Total Payout = ";
print "<font color=FF0000><B>";
printf "\$%.2f" ,$total;
print "</B></font></td></tr>";
print "</table>";

What are some other ways I could have dealt with this problem if I wanted to limit the amount of lines in the script?

Thanks again Marcus :-)



zanardi
journeyman

May 22, 2001, 12:08 PM

Post #4 of 15 (3564 views)
Re: Creating 0's after decimals [In reply to] Can't Post

simple, use the

Code
sprintf

function:


Code
$total = $count * .03; 
$total = sprintf("\$%.2f",$total);

print qq(<tr><td width="50%"><B>Total Payout = <font color="#FF0000"><B>$total</B></font></td></tr>
</table>);

----------------------
Fueled By ZCom


xcom
stranger

May 22, 2001, 3:26 PM

Post #5 of 15 (3554 views)
Re: Creating 0's after decimals [In reply to] Can't Post

Thank You zanardi!
Exactly what I was looking for!

Do you happen to know of any good sites with perl tutorials I could visit to improve my scripting skills?
Seems Like they are all either too basic or too advanced for me. At least the ones I have checked out so far. I use to really like webreview.com but last time I checked,their perl tutorials were gone.

Anyways,thank you all for your help! Its much appreciated!



randor
User

May 22, 2001, 3:36 PM

Post #6 of 15 (3551 views)
Re: Creating 0's after decimals [In reply to] Can't Post

xcom,

im not sure, but i "think" that there are some perl tutorials at www.devshed.com try looking there.. personally i taught myself perl, and the book i used was "perl for Dummies" and i will tell you, it was the best investment i ever made.. i picked it up so fast.. the explanations in that book are so easy my 5 yr old could almost read and understand it.

i hope this helps..

Randor



xcom
stranger

May 22, 2001, 3:52 PM

Post #7 of 15 (3549 views)
Re: Creating 0's after decimals [In reply to] Can't Post

Oh yeah...one other thing if you dont mind!
I have seen the print qq statement many times,but dont quite understand its function. Can you explain to a newbie like myself exactly what print qq is telling perl?

Thanks, xcom



xcom
stranger

May 22, 2001, 3:58 PM

Post #8 of 15 (3548 views)
Re: Creating 0's after decimals [In reply to] Can't Post

Thanks randor!
I will pick that book up tomorrow!
Sound like they made the book just for me! (Perl For Dummies)!

Man.....I love scripting but sure wish I had a better grasp of perl! I could write 4 scripts in the time it takes me to write 1!

Ever wish you could jump from the bottom rung on the ladder straight to the top???!!!! LOL


Thanks guys!.....Much appreciated.

...xcom



randor
User

May 22, 2001, 5:14 PM

Post #9 of 15 (3543 views)
Re: Creating 0's after decimals [In reply to] Can't Post

xcom,

no problem on the help with the book, it is why i come here.. to help as i was helped when i started.. and yes, i wished the same thing when i was learning.. and infact i still learn everyday....

print qq

the qq basically means "quote word", how this works is:

print qq~ hello xcom, i am writing to you\n~;

is the EXACT same as:

print " hello xcom, i am writing to you\n";

the difference is that with print qq~

we can
just keep
writing
on many
lines
without
having
to keep
using the
print ""
statement
each line.
plus with the qq it will allow you to add variables.. like so:
joe has a $object...\n~;

the trick with qq is that whatever the next character is after the first qq is what perl will look for to end the quote.. so:

print qq~ (which is what alot of people use to quote with) will print everything until it finds another ~ and dont forget the ;

now, if you did this:
print qq| it would print everything until it finds another |

i hope this helps..



xcom
stranger

May 22, 2001, 9:44 PM

Post #10 of 15 (3535 views)
Re: Creating 0's after decimals [In reply to] Can't Post

randor,
Thanx!!!
That just helped me with an important part of my script!
I had to write into my script some code that would create a new cgi file every time someone signed up and that little bit of info just helped me put that all together!



freddo
User

May 23, 2001, 5:39 AM

Post #11 of 15 (3527 views)
How to search... [In reply to] Can't Post

Hello XCom,

I know there's not so much help in saying RTFM (Read The [censored] Manual) but i think that peoples should really have a look the documentation. I'm quite sure your version of perl come with the perldoc program.

If you try perldoc --help at your favourite shell, command.com or cmd.exe, you'll get really usefull informations. A good start is:perldoc perl.

Now there is some good options to know about, for example, to search the perl's FAQs, you can use the -q. Where, a funny example, in perldoc -q decimal you would find this:
To get rid of the superfluous digits, just use a format (eg, "printf("%.2f", 19.95)") to get the required precision. See the Floating-point Arithmetic entry in the perlop manpage.

Another one is to check what does a perl's function, like in (another fortuituous example Smile) perldoc -f qq which gives:
qq/STRING/
qr/STRING/
qx/STRING/
qw/STRING/
Generalized quotes. See the Regexp Quote-Like Operators entry in the perlop manpage.


Now you saw three references to the perlop man page (if you checked perldoc perl), and I'm sure you will come, by yourself to where you need to look to really understand how qq// works.

Another way to search the perl's documentation is with the -r switch, but again you should know it since you tried perldoc --help earlier Smile.

Also have a look on how to search the displayed informations by perldoc (in general you type /some regex with the slash to get the prompt).

I mean no offense with this message, but i just wanted to explain to people in general, how i find answers to the general questions I ask myself about perl. It's far more faster to know how to find by yourself than to wait for peoples in a forum to reply.

Btw, for your last question, if you dont find what you need with perldoc, try perldoc perlbook Smile.

For a good tutorial for perl look there. Or learn how to search and you'll be able to find the incredible treasures hidden deep in the net...

I hope this really helps,
else dont hesitate to ask other questions here.
freddo

;---
Real programmers don't eat cache.


zanardi
journeyman

May 23, 2001, 12:56 PM

Post #12 of 15 (3523 views)
Books [In reply to] Can't Post

And about books, go to www.bookpool.com and look for perl books by O'Reilly. Search and see which one you would like the best. I hear all of them are exellent. bookpool.com also has great prices

----------------------
Fueled By ZCom


randor
User

May 23, 2001, 2:57 PM

Post #13 of 15 (3521 views)
Re: Books [In reply to] Can't Post

i have not shopped at bookpool, but my experience has been with www.half.com and it has books (used) for about half (go figure) the regular price..

just another option.

hope this helps...
Randor



freddo
User

May 23, 2001, 3:05 PM

Post #14 of 15 (3519 views)
Re: Books [In reply to] Can't Post

Hi Zanardy,

you didnt checked my last link, did you? Smile

freddo

;---
Real programmers don't eat cache.


zanardi
journeyman

May 24, 2001, 11:47 AM

Post #15 of 15 (3511 views)
Re: Books [In reply to] Can't Post

um yeah I did..

bookpool has new books, for less than what you would normally find them too

and they have a bunch of perl books. Just got look

----------------------
Fueled By ZCom

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives