# Strict conversion of strings to ints?

**URL:** <https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016>\
**Category:** Ruby\
**Created:** [August 26, 2007, 5:19am UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016 "2007-08-26T05:19:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 5:19am UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/1 "2007-08-26T05:19:53Z")

</div>

Is there an easy way to parse a string to an integer that will throw an  
error of some sort if the string is not actually a properly formatted  
integer? “to\_i” of course is very non-strict, which is no good for what  
I want, and I’d prefer not to write my own when it seems likely that  
there would be some way to do this already…

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 5:26am UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/2 "2007-08-26T05:26:53Z")

</div>

On Aug 25, 2007, at 10:19 PM, Kenneth McDonald wrote:

> Is there an easy way to parse a string to an integer that will  
> throw an error of some sort if the string is not actually a  
> properly formatted integer? “to\_i” of course is very non-strict,  
> which is no good for what I want, and I’d prefer not to write my  
> own when it seems likely that there would be some way to do this  
> already…

> > Integer(“junk”)  
> > ArgumentError: invalid value for Integer: “junk”  
> > from (irb):1:in `Integer’  
> > from (irb):1

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/3 "2007-08-26T22:28:02Z")

</div>

The real problem is:

irb(main):002:0\> “12,345”.to\_i  
=\> 12

Basically, Ruby converts anything that starts with an int, which is  
really quite nasty in a case such as the above. I spent a lot of time  
tracking down that problem.

So, I want an integer conversion function that throws an error if the  
_entire_ string can’t be interpreted as an integer; the Ruby analog to  
the standard string-to-int conversion function in most other languages.

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/4 "2007-08-26T22:40:04Z")

</div>

On 26-Aug-07, at 4:32 PM, James Edward G. II wrote:

> > So, I want an integer conversion function that throws an error if  
> > the _entire_ string can’t be interpreted as an integer; the Ruby  
> > analog to the standard string-to-int conversion function in most  
> > other languages.
> 
> raise “Bad integer” unless i\_str =~ /\A-?\d+\z/

What about

int = Integer(i\_str)

?

It accepts leading whitespace, but apart from that it’s pretty picky.

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/5 "2007-08-26T22:43:07Z")

</div>

Kenneth McDonald wrote:

> James Edward G. II wrote:
> 
> > > > Integer(“junk”)
> > 
> > ArgumentError: invalid value for Integer: “junk”

> So, I want an integer conversion function that throws an error if the  
> _entire_ string can’t be interpreted as an integer; the Ruby analog to  
> the standard string-to-int conversion function in most other languages.

Ehrm,

> > Integer(“12,345”)  
> > ArgumentError: invalid value for Integer: “12,345”

That’s exactly what you wanted, right? So what’s the problem?

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/6 "2007-08-26T22:59:09Z")

</div>

James Edward G. II wrote:

> raise “Bad integer” unless i\_str =~ /\A-?\d+\z/

Depending on your needs (do you want to accept all the strings that ruby  
accepts as integers?), you might want to add \_ to that regex (also,  
preceding ‘+’ char):

irb(main):001:0\> i\_str = “12\_345”  
=\> “12\_345”  
irb(main):002:0\> raise “Bad integer” unless i\_str =~ /\A-?\d+\z/  
RuntimeError: Bad integer  
from (irb):2  
irb(main):003:0\> Integer(i\_str)  
=\> 12345

But getting that right is harder than just using Integer().

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/7 "2007-08-26T22:33:04Z")

</div>

On Aug 26, 2007, at 3:26 PM, Kenneth McDonald wrote:

> the _entire_ string can’t be interpreted as an integer; the Ruby  
> analog to the standard string-to-int conversion function in most  
> other languages.

raise “Bad integer” unless i\_str =~ /\A-?\d+\z/

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 26, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/strict-conversion-of-strings-to-ints/113016/8 "2007-08-26T23:43:14Z")

</div>

Mike S. wrote:

> > > Basically, Ruby converts anything that starts with an int, which is  
> > > What about
> 
> int = Integer(i\_str)
> 
> ?
> 
> It accepts leading whitespace, but apart from that it’s pretty picky.
> 
> Mike  
> This is what I was looking for. There’s nothing wrong with the regexp  
> solution of course, it’s just that Simpler is Nicer :-). Many thanks to  
> both of you.

Cheers,  
Ken
