How to check string is numeric or not

Hi friends
I want to check the value is numeric or not
For example
when i am checking numeric or not am expecting as below results
“34” => true
34 => true
“34sdfds” => it should return false
34.90 => false

am using ruby 1.8.7. can anyone suggest isthere is anymethod exist to
check these

On Thu, 12 Jul 2012 19:16:08 +0900
Lucky Nl [email protected] wrote:

check these
def is_numeric(o)
true if Integer(o) rescue false
end


Sincerely yours,
Aleksey V. Zapparov A.K.A. ixti
FSF Member #7118
Mobile Phone: +34 677 990 688
Homepage: http://www.ixti.net
JID: [email protected]

*Origin: Happy Hacking!

On Thu, Jul 12, 2012 at 12:16 PM, Lucky Nl [email protected] wrote:

I want to check the value is numeric or not

According to your example you want to check whether a string
represents an integer value (floats are numeric, too).

For example
when i am checking numeric or not am expecting as below results
“34” => true
34 => true
“34sdfds” => it should return false
34.90 => false

You can use Integer() to do that:

Ruby version 1.8.7
irb(main):001:0> %w{34 34sdfds 34.90}.each do |s| printf “%p: %p\n”,
s, (Integer(s) rescue false) end
“34”: 34
“34sdfds”: false
“34.90”: false
=> [“34”, “34sdfds”, “34.90”]

But actually you do not need a separate check, just use Integer to
convert the string to a Fixnum / Bignum and deal with the exception.

Kind regards

robert

Aleksey Z. wrote in post #1068416:

On Thu, 12 Jul 2012 19:16:08 +0900
Lucky Nl [email protected] wrote:

check these
def is_numeric(o)
true if Integer(o) rescue false
end

This may not be exactly what is wanted:

Integer(“0x123”)
=> 291

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

Also, note that “is_numeric” isn’t an ideal name, because Numeric
includes Float.

Float.ancestors
=> [Float, Precision, Numeric, Comparable, Object, Kernel]

Am 12.07.2012 21:46, schrieb Brian C.:

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

or

/\A\d+\z/ === o

which returns true or false (instead of 0 or nil)

On 07/12/2012 10:30 PM, [email protected] wrote:

Am 12.07.2012 21:46, schrieb Brian C.:

If you just want to allow digits and nothing else, use a regexp:
/\A\d+\z/ =~ o

or

/\A\d+\z/ === o

which returns true or false (instead of 0 or nil)

If o is allowed to be a Fixnum, you need to cast to string first:

/\A\d+\z/ === o.to_s

Alternatively

!!o.to_s[/\A\d+\z/]

On Fri, Jul 13, 2012 at 10:19 AM, Lars H.
[email protected] wrote:

If o is allowed to be a Fixnum, you need to cast to string first:

/\A\d+\z/ === o.to_s

WTF? (That’s the first time I remember me writing this here.)

Let me get that straight: you want to convert something which you
know is an integer to a String to check whether it is an integer?

If o is allowed to be an integer type then you would better check that
directly (e.g. with Integer === o). But it get’s better:

irb(main):010:0> a=[1,1<<50,“1”]
=> [1, 1125899906842624, “1”]
irb(main):011:0> a.each {|x| y=Integer(x); printf “type %p converted
%p type %p\n”, x.class, y, y.class}
type Fixnum converted 1 type Fixnum
type Bignum converted 1125899906842624 type Bignum
type String converted 1 type Fixnum
=> [1, 1125899906842624, “1”]

Cheers

robert

Am 13.07.2012 11:41, schrieb Lars H.:

On 07/13/2012 11:08 AM, Robert K. wrote:
Not quite. The question was about checking a value that could be a
string, integer, float or presumably anything, and return true if the
value contains digits only, false otherwise. At least that’s how I
interpret the message starting this thread.

seems the original post is not very clear,
the subject line implies only strings, the post itself does not.

On 07/13/2012 11:08 AM, Robert K. wrote:

know is an integer to a String to check whether it is an integer?
Not quite. The question was about checking a value that could be a
string, integer, float or presumably anything, and return true if the
value contains digits only, false otherwise. At least that’s how I
interpret the message starting this thread.

Why not simply:

$num_str = gets.chomp
puts $num_str =~ /^\d+$/?“True”:“False”

or as a method in a class:

class Test1
def initialize(a_num)
puts is_num_str?(a_num)?“True”:“False”
end
def is_num_str?(num)
num =~ /^\d+$/?true:False
end
end

Test1.new(gets.chomp)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs