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:
round function

 



chomdang
Deleted

Aug 8, 2000, 5:22 AM

Post #1 of 4 (593 views)
round function Can't Post

Hi everyone....
When I do a division in perl (that's not complicate), I got a floating number... Ex.: " 23.445678 ". I want to know the way to put this number in a HTML page (that's ok) but I want the number to have only 2 decimals ( a round function), so the number you become " 23.45 ". I know that in perl we can use the printf and sprintf function but in Html, I don't know how to do it.
Thanks in advance
Dang


rGeoffrey
User / Moderator

Aug 8, 2000, 11:48 AM

Post #2 of 4 (593 views)
Re: round function [In reply to] Can't Post

What do you mean by "but in Html, I don't know how to do it"? How are you doing calculations if you are in HTML?

If you are building an HTML page with a perl script, you can just create the string with the number and print it...

$string = sprintf ("%5.2f", $num);

...

print $string;

[This message has been edited by rGeoffrey (edited 08-08-2000).]


chomdang
Deleted

Aug 8, 2000, 12:57 PM

Post #3 of 4 (593 views)
Re: round function [In reply to] Can't Post

Thanks, you were right....


Cure
User

Aug 8, 2000, 2:11 PM

Post #4 of 4 (593 views)
Re: round function [In reply to] Can't Post

 You have 3 options
1.JAVASCRIPT
2.PHP
3.PERL(I would assume you want Perl because your at a perl site)

JAVASCRIPT
-----------
<HTML>
<BODY BGCOLOR="#FFFFFF">
<SCRIPT LANGUAGE="JavaScript">
<!--
var number = 123.42845;
document.write (Math.round(number * 100) / 100);
//-->
</SCRIPT>
</BODY>
</HTML>


PHP
-----
<HTML>
<BODY BGCOLOR="#FFFFFF">
<?php
$num = 12.3478899;
printf("%.2f",$num);
?>
</BODY>
</HTML>


Perl<-- THE REAL LANGUAGE<-- the BEST LANGUAGE <-- accept_it
------------------------------------------------------------

use a SSI tag to call the script that will round to 2 decimal places OR put the html and the round to 2 decimal places all in the same script <---- UP to you

SSI == <!--#exec cgi="cgi-bin/decimal.pl"-->
put that SSI tag in your html page AND make the extensionof html page to .shtml(that tells the server to parsr the page)

decimal script-->
----------------
#!/usr/bin/perl;
print "Content-type: text/html\n\n";
$num = 12.3478899;
printf("%.2f",$num);


OR --> just put your html in the decimal script...
--------------------------------------------------
#!/usr/bin/perl;
print "Content-type: text/html\n\n";
print qq~ <html><body>
$num = 12.3478899;
printf("%.2f",$num);
</body></html>
~;

Cure

 
 


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

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