# Inconsistent Behavior Converting String to Integer/Float

**URL:** <https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192>\
**Category:** Ruby\
**Created:** [February 25, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192 "2006-02-25T16:07:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 25, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/1 "2006-02-25T16:07:21Z")

</div>

$ruby-yarv -v  
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]  
YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts:]

$ruby -v  
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

# Correct behavior

num = “+3”  
Float(num) # -\> 3.0  
Integer(num) # -\> 3

# Strange behavior

num = “3e2”  
Float(num) # -\> 300.0  
Integer(num) # -\> ArgumentError

Why is an ArgumentError raised in the second example?

OT: ruby-talk seems to be rejecting my email. I sent this twice, and  
both times it did not make it to the list.

– Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 25, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/2 "2006-02-25T16:17:24Z")

</div>

> > > > > “D” == Daniel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> $ruby-yarv -v  
D\> ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [powerpc-darwin8.5.0]  
D\> YARVCore 0.4.0 Rev: 466 (2006-02-22) [opts:]

You have a problem with your version of yarv

uln% ruby -ve ‘num = “+3”; p num, Float(num), Integer(num)’  
ruby 1.9.0 (2006-02-24) [x86\_64-linux]  
“+3”  
3.0  
3  
uln%

uln% ruby-yarv -ve ‘num = “+3”; p num, Float(num), Integer(num)’  
ruby 2.0.0 (Base: Ruby 1.9.0 2006-02-14) [x86\_64-linux]  
YARVCore 0.4.0 Rev: 475 (2006-02-23) [opts:]  
“+3”  
3.0  
3  
uln%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 25, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/3 "2006-02-25T16:30:54Z")

</div>

ts wrote:

> You have a problem with your version of yarv

I was asking specifically about the second example:

num = “3e2”  
Float(num) # -\> 300.0  
Integer(num) # -\> ArgumentError

> Guy Decoux

– Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 25, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/4 "2006-02-25T16:38:34Z")

</div>

> > > > > “D” == Daniel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> I was asking specifically about the second example:

OK, sorry, I’ve not understood.

Even 1.6.8 give the same result

svg% ./ruby -ve ‘Integer(“3e2”)’  
ruby 1.6.8 (2002-12-24) [i686-linux]  
-e:1:in `Integer’: invalid value for Integer: “3e2” (ArgumentError)  
from -e:1  
svg%

1.4.2 give a strange result 🙂

Guy Decoux

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 27, 2006, 1:22am UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/5 "2006-02-27T01:22:56Z")

</div>

I also find using a capatilized method name very odd and  
un-rubyish. Looking through the docs, I see there is Array(),  
Float(), Integer(), String(), URI(), DelegateClass(), and  
possibly more. Why is this style used over Class#new?

– Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [February 27, 2006, 12:58am UTC](https://www.ruby-forum.com/t/inconsistent-behavior-converting-string-to-integer-float/73192/6 "2006-02-27T00:58:41Z")

</div>

Hi,

In message “Re: Inconsistent Behavior Converting String to  
Integer/Float”  
on Sun, 26 Feb 2006 00:07:36 +0900, Daniel H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|# Strange behavior  
|num = “3e2”  
|Float(num) # → 300.0  
|Integer(num) # → ArgumentError

Integer() does not understand scientific notation as an integer  
representation.

```
						matz.

```
