Forum: Ruby how to check string is numeric or not

5de34f53b21201c126dccbb82b2ec77c?d=identicon&s=25 Lucky Nl (lucky1234)
on 2012-07-12 12:16
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
Aae6d4accf06084efc742f85d3806b33?d=identicon&s=25 Aleksey Zapparov (aleksey_z)
on 2012-07-12 12:46
(Received via mailing list)
Attachment: signature.asc (489 Bytes)
On Thu, 12 Jul 2012 19:16:08 +0900
Lucky Nl <lists@ruby-forum.com> 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: zapparov@jabber.ru

*Origin: Happy Hacking!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2012-07-12 15:11
(Received via mailing list)
On Thu, Jul 12, 2012 at 12:16 PM, Lucky Nl <lists@ruby-forum.com> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2012-07-12 21:46
Aleksey Zapparov wrote in post #1068416:
> On Thu, 12 Jul 2012 19:16:08 +0900
> Lucky Nl <lists@ruby-forum.com> 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]
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2012-07-12 22:31
(Received via mailing list)
Am 12.07.2012 21:46, schrieb Brian Candler:
> 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)
C608970872a3048fd3077c001f44929b?d=identicon&s=25 Lars Haugseth (Guest)
on 2012-07-13 10:20
(Received via mailing list)
On 07/12/2012 10:30 PM, sto.mar@web.de wrote:
> Am 12.07.2012 21:46, schrieb Brian Candler:
>> 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/]
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2012-07-13 11:10
(Received via mailing list)
On Fri, Jul 13, 2012 at 10:19 AM, Lars Haugseth
<ruby-talk@larshaugseth.com> 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
C608970872a3048fd3077c001f44929b?d=identicon&s=25 Lars Haugseth (Guest)
on 2012-07-13 11:42
(Received via mailing list)
On 07/13/2012 11:08 AM, Robert Klemme 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.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2012-07-13 15:17
(Received via mailing list)
Am 13.07.2012 11:41, schrieb Lars Haugseth:
> On 07/13/2012 11:08 AM, Robert Klemme 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.
09e0f1a72ad8e1d4fc45434699e8026f?d=identicon&s=25 Roberto Salcedo (totochto)
on 2014-06-11 05:17
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)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.