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:
truncating decimals

 



neophyte
stranger

May 16, 2001, 12:06 PM

Post #1 of 4 (779 views)
truncating decimals Can't Post

I am new to perl, and I have been thrown the task of creating a cgi that formats form info, prints it out to a flat file and assigns the file a random number. I have everything working properly. however, I used the rand function to generate random numbers, and all of the numbers contain decimals. How can I trim off all numbers after the decimal? I looked into the split function, from what I've seen, it is for strings. my random number is assigned to a variable name and split (at least the way I've tried using it) will not work. any suggestions?



randor
User

May 16, 2001, 12:14 PM

Post #2 of 4 (778 views)
Re: truncating decimals [In reply to] Can't Post

try this:

$decimal_number = '12.233';

$decimal_number = sprintf("%.0f",$decimal_number );

print $decimal_number ;



neophyte
stranger

May 16, 2001, 1:30 PM

Post #3 of 4 (775 views)
Re: truncating decimals [In reply to] Can't Post

Thanks!



freddo
User

May 16, 2001, 2:43 PM

Post #4 of 4 (767 views)
Re: truncating decimals [In reply to] Can't Post

Hi Neo,

You can also have a look to the int function.

Code
print int rand 5;

will print an interger between 0 and 5.

i hope this helps
Freddo

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

 
 


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

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