Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • is_numeric

    Posted on August 8th, 2009 RubyLove 3 comments

    The is_numeric() function allows you to check if a particular variable is a numeric string. Numeric strings can consist of a sign, any number of digits, a decimal part and an exponential part.

    PHP

    $my_string = '+1.998';
    var_dump( is_numeric($string1) );
    // => true

    Ruby

    def is_numeric?(num)
        true if Float(num) rescue false;
    end
     
    my_string = '+1.985';
    puts is_numeric?(my_string);
    # => true