# Floating point conversion error?

**URL:** <https://www.ruby-forum.com/t/floating-point-conversion-error/126939>\
**Category:** Ruby\
**Created:** [January 23, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939 "2008-01-23T16:25:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhoult](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bhoult](https://www.ruby-forum.com/u/bhoult)\
**Post date:** [January 23, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939/1 "2008-01-23T16:25:59Z")

</div>

> > (2241.18 \* 100).to\_i  
> > =\> 224117

Anyone know what is up with that?

---

<div class="post-metadata">

**Author:** ![bhoult](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bhoult](https://www.ruby-forum.com/u/bhoult)\
**Post date:** [January 23, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939/2 "2008-01-23T16:32:50Z")

</div>

Brandon H. wrote:

> > > (2241.18 \* 100).to\_i
> 
> =\> 224117
> 
> Anyone know what is up with that?

Floating point arithmetic isn’t entirely accurate.

> > “%.30f” % (2241.18 \* 100)  
> > =\> “224117.999999999970896169543266296387”

And since 224117.999999999970896169543266296387.to\_i is 224117, that’s  
what  
you get.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![bhoult](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bhoult](https://www.ruby-forum.com/u/bhoult)\
**Post date:** [January 23, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939/3 "2008-01-23T16:34:14Z")

</div>

Sebastian H. wrote:

> Brandon H. wrote:
> 
> > > > (2241.18 \* 100).to\_i
> > 
> > =\> 224117
> > 
> > Anyone know what is up with that?
> 
> Floating point arithmetic isn’t entirely accurate.
> 
> > > “%.30f” % (2241.18 \* 100)  
> > > =\> “224117.999999999970896169543266296387”
> 
> And since 224117.999999999970896169543266296387.to\_i is 224117, that’s  
> what  
> you get.
> 
> HTH,  
> Sebastian

Thanks… found this after poking around… guess I should have spent  
more time looking.

[http://www.ruby-forum.com/topic/132986#new](http://www.ruby-forum.com/topic/132986#new)

---

<div class="post-metadata">

**Author:** ![bhoult](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bhoult](https://www.ruby-forum.com/u/bhoult)\
**Post date:** [January 23, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939/4 "2008-01-23T16:35:12Z")

</div>

Hello!

On Jan 23, 2008 4:25 PM, Brandon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > (2241.18 \* 100).to\_i  
> > > =\> 224117
> 
> Anyone know what is up with that?

Floating point numbers are never 100% accurate. See  
[Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point) for a detailed  
explanation.

---

<div class="post-metadata">

**Author:** ![bhoult](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bhoult](https://www.ruby-forum.com/u/bhoult)\
**Post date:** [January 23, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/floating-point-conversion-error/126939/5 "2008-01-23T16:38:45Z")

</div>

On Jan 23, 2008, at 10:25 AM, Brandon H. wrote:

> > > (2241.18 \* 100).to\_i  
> > > =\> 224117
> 
> Anyone know what is up with that?

What Every Computer Scientist Should Know About Floating-Point  
Arithmetic

[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)
