CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr
 MAIN INDEX SEARCHPOSTS WHO'S ONLINE LOG IN

Home: Perl Programming Help: Beginner:
Converting Real Number to Integers

kencl
User

Feb 4, 2001, 9:10 PM

Post #1 of 3 (283 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 (280 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 (267 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

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

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0