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:
rounding numbers to 2 decimal places?

 



kind1
Deleted

Feb 24, 2000, 9:58 PM

Post #1 of 2 (643 views)
rounding numbers to 2 decimal places? Can't Post

Basic question ... .

if a number such as 124.316343 is stored in $number, how do i round it off to 2 decimal places? (to 124.32)

thanks


Jasmine
Administrator / Moderator

Feb 24, 2000, 11:35 PM

Post #2 of 2 (643 views)
Re: rounding numbers to 2 decimal places? [In reply to] Can't Post

To round numbers, you can use the sprint function.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$a = 124.316343;
$b = sprintf("%.2f", $a);

print $b;

# prints 124.32
</pre><HR></BLOCKQUOTE>

You can change the number of decimal places by changing the 2 in "%.2f" to the number of decimal places you want to use.

 
 


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

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