# Re: Problem with MODULO function?

**URL:** <https://www.ruby-forum.com/t/re-problem-with-modulo-function/59454>\
**Category:** Ruby\
**Created:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/re-problem-with-modulo-function/59454 "2006-05-03T19:07:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan\_aerts\_SRI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8c91f0/32.png) [@jan\_aerts\_SRI](https://www.ruby-forum.com/u/jan_aerts_SRI)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/re-problem-with-modulo-function/59454/1 "2006-05-03T19:07:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jan\_aerts\_SRI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8c91f0/32.png) [@jan\_aerts\_SRI](https://www.ruby-forum.com/u/jan_aerts_SRI)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/re-problem-with-modulo-function/59454/2 "2006-05-03T19:07:11Z")

</div>

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 ☹ )
