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:
Converting Real Number to Integers

 



kencl
User

Feb 4, 2001, 9:10 PM

Post #1 of 3 (246 views)
Converting Real Number to Integers Can't Post

I tried:

Code
$width = # a real number with lots of decimal places 
$percent = # a real number with lots of decimal places
use integer # switch to integer mode
$width /= 1; $percent /= 1; # converts reals to integers <- obviously not!
$percent .= '%'; # single quotes arn't subject to interpolation, no need to escape the percent sign
no integer # back to real number mode
print "width is $width, and percent is $percent.";

result:
width is 35.7142857142857, and percent is 6%.

How do I force $width to be an integer? Why doesn't PERL just have a DIV operator to complement the MOD operator anyway?

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


Kanji
User / Moderator

Feb 5, 2001, 1:46 AM

Post #2 of 3 (243 views)
Re: Converting Real Number to Integers [In reply to] Can't Post

The /= does work for me when I use integer; as you have, but the more common way of forcing integers is to use int() ...

Code
$width = int( $width / 1 );



kencl
User

Feb 5, 2001, 9:13 PM

Post #3 of 3 (230 views)
Re: Converting Real Number to Integers [In reply to] Can't Post

Thanks for the info. No idea why use integer didn't work for me.

I ended up using sprintf:

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

The neat thing is that it didn't return the decimal point either Cool

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!

 
 


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

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