# A bug of validates\_numericality\_of?

**URL:** <https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730>\
**Category:** Rails\
**Created:** [November 30, 2007, 10:22am UTC](https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730 "2007-11-30T10:22:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [November 30, 2007, 10:22am UTC](https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730/1 "2007-11-30T10:22:05Z")

</div>

Hi,

In code like this

validates\_numericality\_of :price

the price is supposed to be a string in number format but can include  
other non-digital characters like ‘a’, since :only\_integer is set to  
false by default, right?

In rails implementation, it use Kernel.Float to complete the work. But  
seems Kernel.Float(ruby 1.8.6) always throw an exception when the  
number string contains non-digital characters, such as “123.00a”. So  
in fact there is no diffierences whether the switch :only\_integer is  
turned on or off.

I found in JRuby, the Kernel.Float will accept string that looks like  
“123.00a” but will finally return a zero.

Do I misunderstand of the :only\_integer switch?

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [November 30, 2007, 10:38am UTC](https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730/2 "2007-11-30T10:38:28Z")

</div>

only\_integer means it will only allow integers. Otherwise, decimal  
places are allowed. You will need to write a custom method to  
validate the “numericality” of strings containing alpha characters.

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [November 30, 2007, 10:46am UTC](https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730/3 "2007-11-30T10:46:06Z")

</div>

On Nov 30, 2007, at 1:21 AM, Jan wrote:

> number string contains non-digital characters, such as “123.00a”. So  
> in fact there is no diffierences whether the switch :only\_integer is  
> turned on or off.
> 
> I found in JRuby, the Kernel.Float will accept string that looks like  
> “123.00a” but will finally return a zero.
> 
> Do I misunderstand of the :only\_integer switch?

Integer would mean 100 vs 100.0

I’m new to Ruby, so maybe there’s something unique, but why would you  
think 123.00a is a float?

[123, 123.00, -123.00].each do |n|  
puts n.is\_a?(Float)  
end

#false  
#true  
#true

Trying to put in a number like 123.00a in that array makes it fail.

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![jan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jan](https://www.ruby-forum.com/u/jan)\
**Post date:** [November 30, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/a-bug-of-validates-numericality-of/121730/4 "2007-11-30T17:31:51Z")

</div>

Thanks Guys, I got the meaning of only\_integer. I found I was  
misleaded by JRuby’s implementation of Kernel.Float - maybe it’s a bug  
of JRuby …
