Forum: Ruby on Rails beer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A9f0c7c44434d7d182c299693ef9be60?d=identicon&s=25 String numecal validate! (Guest)
on 2006-03-29 20:41
Hello, all


I have one string in my controller but i need to knwon how i verify if
it is numerical


Thanks...
Daa859590270ab904aa3460357750397?d=identicon&s=25 Kevin Elhart (kelhart)
on 2006-03-29 20:57
Regexp to the rescue:

if s =~ /[0-9]+/
# I'm an integer, do something with me
end

And you can go from there, something like:

if s =~ /-?[0-9]+\.[0-9]+/
# I'm a postive/negative float
end

and further:

if s =~ /-?[0-9]+(\.[0-9]+)/
# I'm a positive/negative integer/float
end

And so on.  Hope I got regexp right.

There might be pre-fab methods that do this, but I'm not sure off the
top of my head.




String numecal  validate! wrote:
> Hello, all
>
>
> I have one string in my controller but i need to knwon how i verify if
> it is numerical
>
>
> Thanks...
This topic is locked and can not be replied to.