Hello, all I have one string in my controller but i need to knwon how i verify if it is numerical Thanks... -- Posted via http://www.ruby-forum.com/.
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...-- Posted via http://www.ruby-forum.com/.
to_f will convert it to a Float, .to_i to an Integer, but these won''t "tell" you whether it''s numerical, in fact they often accept strings that have some digits in them but also other characters. The better solution might be to use a regular expression, the content of which will be entirely dependent on what type of number you are expecting. Sean Cribbs Web Services - KCKCC>>> blerloko@hotmail.com 3/29/2006 12:41 PM >>>Hello, all I have one string in my controller but i need to knwon how i verify if it is numerical Thanks... -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/d383157a/attachment.html
> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > String numecal validate! > Sent: Wednesday, March 29, 2006 11:41 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] beer > > > Hello, all > > > I have one string in my controller but i need to knwon how i > verify if it is numericalif string =~ /^\d+$/ Another option is to use Integer(str) and check for an error. What this has to do with beer, I''m not sure. Regards, Dan