Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Determine If A String Is Numeric In Ruby

DZone's Guide to

Determine If A String Is Numeric In Ruby

·
Free Resource
# Determine if a string is numeric using Float and rescue
# Revised long version from http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Ruby


def numeric?(s)
  begin
    Float(s)
  rescue
    false # not numeric
  else
    true # numeric
  end
end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}