Forum: Ruby How I can check number?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ilyas (Guest)
on 2006-06-01 13:41
How I can to check float value, with regular expression. Also need check
a integer with float values.
Ilyas (Guest)
on 2006-06-01 14:21
How I can simplify it:
params[:value] =~ /^\d+$|^-\d+$|^\d+\.\d+$|^-\d+\.\d+$/
Robert K. (Guest)
on 2006-06-01 14:38
(Received via mailing list)
2006/6/1, Ilyas <removed_email_address@domain.invalid>:
> How I can to check float value, with regular expression. Also need check
> a integer with float values.

The easiest check is to use Float and Integer:

>> Float("foo") rescue false
=> false
>> Float("1") rescue false
=> 1.0
>> Integer("10") rescue false
=> 10
>> Integer("10.4") rescue false
=> false

Kind regards

robert
Dave B. (Guest)
on 2006-06-01 15:30
(Received via mailing list)
Ilyas wrote:
> How I can simplify it:
> params[:value] =~ /^\d+$|^-\d+$|^\d+\.\d+$|^-\d+\.\d+$/

/^ [-+]? \d+ (\. \d+)? $/x


Cheers,
Dave
This topic is locked and can not be replied to.