Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
ruby equivalent PHP function is_numeric?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jason Dusek, post: 4536961"] There is no such function, although you can pretty readily use regexes to do it... It really is too bad that String.to_f does not throw an exception - or at least return NaN - when the string is not a number. A regex that matches the cases where String.to_f will find a value is simple to write, and you can create String.numeric? like so: class String def numeric? not ( self =~ /^[[:digit:]]+(\.[[:digit:]]+)?([eE][[:digit:]]+)?/ or self =~ /^\.[[:digit:]]+([eE][[:digit:]]+)?/ ).nil? end end [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
ruby equivalent PHP function is_numeric?
Top