Re: Problem with MODULO function?


#1

Hi Peter,

I think it has to do with the fact that numberofpages is a string
instead of an integer. In the irb:

number_of_pages = 5
number_of_pages % 2 => 1
number_of_pages = ‘5’
number_of_pages % 2 => 5

So the test (numberofpages % 2 != 0) will always return a false, unless
numberofpages was 0 in the first place.

Workaround: use ( numberofpages.to_i % 2 ) instead.

Jan.


#2

On Wed, 2006-05-03 at 21:21 +0900, jan aerts (RI) wrote:

numberofpages was 0 in the first place.

Yes, String#% does something completely different:

=> “16768.181”

“%.3f” % 16768.181202317

=> “16768.181”

“%d” % 502

=> “502”

“An example of %s with %s” % [“formatting”, “String#%”]

=> “An example of formatting with String#%”

ri ‘String#%’
--------------------------------------------------------------- String#%
str % arg => new_str

 Format---Uses _str_ as a format specification, and returns the
 result of applying it to _arg_. [...snipped...]

(P.s. seems to be a lot of top-posting going on these days :frowning: )