# Any body can help me ? why is this happen?

**URL:** <https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072>\
**Category:** Rails\
**Created:** [March 31, 2010, 8:20am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072 "2010-03-31T08:20:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 8:20am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/1 "2010-03-31T08:20:16Z")

</div>

Hii All

I use ruby 1.8.7  
I run irb and type:

a = (0.29 \* 100).to\_i

the result is 28

why???

could u explain to me… Please???

THank you

–

Senior Rails Developer  
Anton Effendi - Wu You Duan

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 8:27am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/2 "2010-03-31T08:27:39Z")

</div>

Scary!

irb(main):019:0\> (0.57 \* 100).to\_i  
=\> 56  
irb(main):020:0\> (0.58 \* 100).to\_i  
=\> 57

This is totally out of my understanding… Will look into core… seems  
like some bug…

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 8:33am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/3 "2010-03-31T08:33:49Z")

</div>

Nm… this explains it:

[http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough](http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough)

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 8:34am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/4 "2010-03-31T08:34:19Z")

</div>

(0.29\*100).to\_s.to\_i will return what you expect.

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/5 "2010-03-31T18:43:47Z")

</div>

Quoting anton effendi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hii All
> 
> I use ruby 1.8.7  
> I run irb and type:
> 
> a = (0.29 \* 100).to\_i
> 
> the result is 28

Here’s why:

irb(main):008:0\> ‘%0.16f’ % (0.29 \* 100)  
=\> “28.9999999999999964”

Most decimal fractions cannot be represented exactly in binary. If you  
need  
them to behave in certain ways, e.g. using real numbers to represent US  
dollars and cents, take care and learn to use the following functions as  
needed.

irb(main):002:0\> (0.29 \* 100).to\_i  
=\> 28  
irb(main):003:0\> (0.29 \* 100).round  
=\> 29  
irb(main):004:0\> (0.29 \* 100).floor  
=\> 28  
irb(main):005:0\> (0.29 \* 100).ceil  
=\> 29  
irb(main):009:0\> (-0.29 \* 100)  
=\> -29.0  
irb(main):010:0\> (-0.29 \* 100).to\_i  
=\> -28  
irb(main):011:0\> (-0.29 \* 100).floor  
=\> -29  
irb(main):012:0\> (-0.29 \* 100).round  
=\> -29  
irb(main):013:0\> (-0.29 \* 100).ceil  
=\> -28

IIRC, .to\_i rounds towards zero, .floor rounds down, .ceil rounds up,  
and  
.round adds 1/2 and rounds towards zero.

HTH,  
Jeffrey

P.S. if you are in the financial field, the SEC has rules on how to do  
arithmetic in US dollars and cents.

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 8:40am UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/6 "2010-03-31T08:40:11Z")

</div>

See this may be u get some idea…  
[http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough](http://rubypond.com/blog/when-ruby-floating-just-isn-t-good-enough)

-Shyam

---

<div class="post-metadata">

**Author:** ![anton\_effendi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@anton\_effendi](https://www.ruby-forum.com/u/anton_effendi)\
**Post date:** [March 31, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/any-body-can-help-me-why-is-this-happen/186072/7 "2010-03-31T19:52:54Z")

</div>

Thank you all  
I found why that happen…

On Wed, Mar 31, 2010 at 11:39 PM, Jeffrey L. Taylor  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Here’s why:  
> irb(main):002:0\> (0.29 \* 100).to\_i  
> =\> -28
> 
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

Senior Rails Developer  
Anton Effendi - Wu You Duan
