# Re: How I can check number?

**URL:** <https://www.ruby-forum.com/t/re-how-i-can-check-number/62196>\
**Category:** Ruby\
**Created:** [June 1, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/re-how-i-can-check-number/62196 "2006-06-01T12:19:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 1, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/re-how-i-can-check-number/62196/1 "2006-06-01T12:19:03Z")

</div>

How I can to check float value, with regular expression. Also need  
check  
a integer with float values.

I do not know exactly what you mean by this:  
do you want to find a string that could represent a float (resp.  
integer)  
number  
in a text ?  
That could be a string made up of

- an arbitrary number of 0,1,…,9 , followed by a dot, followed by an  
arbitrary number  
of 0,1,2,…,9 in case of a float number,
- an arbitrary number of 0,1,…,9 in case of an integer number.

Then, you could do this

class String  
def scan\_for\_numbers  
int\_regexp=/+_-_[0-9]+/  
fl\_regexp=/+_-_[0-9]+.[0-9]+/

```
     temp=self.dup

```

floats=temp.scan(fl\_regexp)  
floats.each{|x| temp.gsub!(x,’’)}  
ints=temp.scan(int\_regexp)  
all\_numbers={}  
floats.each{|x| all\_numbers.update({x,’ is a floating point  
number!’})}  
ints.each{|x| all\_numbers.update({x,’ is an integer number!’})}  
return all\_numbers  
end  
end

a\_long\_text=‘my bank account is -100000000.09 dollars, but I earn 1  
dollar a  
day, which is better than earning 0.50 dollars or 0 dollars.’

b=a\_long\_text.scan\_for\_numbers  
puts b

* * *

gives:  
-100000000.09 is a floating point number!0 is an integer number!1 is an  
integer number!0.50 is a floating point number!

Best regards,

Axel
