# How to check if a given string is a valid float value or not

**URL:** <https://www.ruby-forum.com/t/how-to-check-if-a-given-string-is-a-valid-float-value-or-not/59882>\
**Category:** Rails\
**Created:** [May 8, 2006, 6:08am UTC](https://www.ruby-forum.com/t/how-to-check-if-a-given-string-is-a-valid-float-value-or-not/59882 "2006-05-08T06:08:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [May 8, 2006, 6:08am UTC](https://www.ruby-forum.com/t/how-to-check-if-a-given-string-is-a-valid-float-value-or-not/59882/1 "2006-05-08T06:08:54Z")

</div>

I can’t use active record or anything. I need to validate inside the  
controller. I thought I could use to\_f in the string method. The  
problem is  
that for a value of 1.3fsfs it returns 1.0 . I thought it would be  
consistently 0.0 . I can get a hack around it but looking for an elegant  
solution.

Thanks.

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [May 8, 2006, 6:24am UTC](https://www.ruby-forum.com/t/how-to-check-if-a-given-string-is-a-valid-float-value-or-not/59882/2 "2006-05-08T06:24:29Z")

</div>

Neeraj K. wrote:

> I can’t use active record or anything. I need to validate inside the  
> controller. I thought I could use to\_f in the string method. The  
> problem is  
> that for a value of 1.3fsfs it returns 1.0 . I thought it would be  
> consistently 0.0 . I can get a hack around it but looking for an elegant  
> solution.
> 
> Thanks.

I think ActiveRecord just tries to call Kernel.Float(string) to see if  
it can be converted properly. This is also how I do it for a similar  
validation.

Jake
