How can I check if a value is a number?

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


I want to simply check if a returned value from a form text field is a number i.e.: 12 , 12.5 or 12.75. Is there a simple way to check this, especially if the value is pulled as a param?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can use

12.is_a? Numeric

(Numeric will work for integers and floats.)

If it arrives as a string that might contain a representation of a valid number, you could use

class String
  def valid_float?
    true if Float self rescue false
  end
end

and then '12'.valid_float? will return true if you can convert the string to a valid float (e.g. with to_f).

Solution 2 - Ruby on-Rails

I usually just use Integer and Float these days.

1.9.2p320 :001 > foo = "343"
 => "343"
1.9.2p320 :003 > goo = "fg5"
 => "fg5"

1.9.2p320 :002 > Integer(foo) rescue nil
 => 343
1.9.2p320 :004 > Integer(goo) rescue nil
 => nil

1.9.2p320 :005 > Float(foo) rescue nil
 => 343.0
1.9.2p320 :006 > Float(goo) rescue nil
 => nil

Solution 3 - Ruby on-Rails

Just regexp it, it's trivial, and not worth thinking about beyond that:

v =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/

(Fixed as per Justin's comment)

Solution 4 - Ruby on-Rails

You can add a:

validates_numericality_of :the_field

in your model.

See: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002172

Solution 5 - Ruby on-Rails

Just convert string twice:

num = '12'
num == num.to_i.to_s 
#=> true 

num = '3re'
num == num.to_i.to_s 
#=> false

Solution 6 - Ruby on-Rails

I would suggest this one

def is_number?
  self.to_f == self
end

> 15.is_number?
=> true
> 15.0.is_number?
=> true
> 'Not a number'.is_number?
=> false
> (0/0.0).is_number?
=> false

Solution 7 - Ruby on-Rails

irb(main):005:0> 1.1.is_a? Numeric
=> true
irb(main):006:0> 1.is_a? Numeric
=> true
irb(main):007:0> 'asd'.is_a? Numeric
=> false

Solution 8 - Ruby on-Rails

There was answer with Kernel#Float

But this variant is used with exception: false key and double bang to return boolean

class String
  def number?
    !!Float(self, exception: false)
  end
end

Solution 9 - Ruby on-Rails

String values always convert to 0 with .to_i

[14] pry(main)> 'Apple'.to_i > 0
=> false

[15] pry(main)> '101'.to_i > 0
=> true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser211662View Question on Stackoverflow
Solution 1 - Ruby on-RailsPeterView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdaesuView Answer on Stackoverflow
Solution 3 - Ruby on-RailscwninjaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsryanprayogoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsVlad HilkoView Answer on Stackoverflow
Solution 6 - Ruby on-RailsinstalleroView Answer on Stackoverflow
Solution 7 - Ruby on-RailsBrian ArmstrongView Answer on Stackoverflow
Solution 8 - Ruby on-RailsmechnicovView Answer on Stackoverflow
Solution 9 - Ruby on-RailsAdrian TehView Answer on Stackoverflow