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:
Decimal Point

 



klgan
stranger

Mar 14, 2001, 12:08 AM

Post #1 of 2 (259 views)
Decimal Point Can't Post

Hi
How do I control the number of decimal points printed using PRINT function? For e.g., if my variable value is 2.14543, but I need the output to be only 2.14

Thank you.



Jean
User


Mar 14, 2001, 2:36 AM

Post #2 of 2 (256 views)
Re: Decimal Point [In reply to] Can't Post

You will have to use the printf() function.
The following example will print the $num,
allocate 3 characters to the integer part of the $num
(replacing missing characters with 0)
and 2 characters to the decimal part

Code
printf("% 03.2f\n", $num);

(Note: there is no space between % and 03)
Results (Original -> Printed):
12.11111111 -> 012.11
1024.3 -> 1024.30
105 -> 105.00
1.5 -> 001.50


For more info see man perlfunc

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


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

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