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

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs