# BigDecimal to float error

**URL:** <https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754>\
**Category:** Ruby\
**Created:** [October 5, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754 "2009-10-05T22:08:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 5, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754/1 "2009-10-05T22:08:59Z")

</div>

Hi,

I found a weird problem.

In ruby 1.8.5, I get the following result in irb.

BigDecimal.new(“86.02”).to\_f #=\> 86.2

I don’t know if this is a ruby bug or machine bug.  
In ruby 1.8.6 on a different machine, it’s ok.

Is this a known issue?

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 5, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754/2 "2009-10-05T22:15:26Z")

</div>

On Oct 5, 1:02 pm, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this a known issue?
> 
> Sam

I narrowed down the problem.  
It has nothing to do with converting to float.

irb(main):023:0\> BigDecimal.new(“1.0234”)  
=\> #BigDecimal:b7efb05c,‘0.1234E1’,8(12)

The internal representation is wrong.

Will upgrading ruby fix this problem?

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 5, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754/3 "2009-10-05T22:27:09Z")

</div>

On Mon, Oct 5, 2009 at 2:05 PM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this a known issue?

You don’t say what patch level of 1.8.5 you are on. Using 1.8.5pl114  
it works fine. However, likely it is a bug where the 0s immediately  
following the decimal get eaten.

Upgrade your Ruby. Don’t use 1.8.5.

Kirk H.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [October 5, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/bigdecimal-to-float-error/176754/4 "2009-10-05T22:31:15Z")

</div>

On Mon, Oct 5, 2009 at 1:15 PM, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > In ruby 1.8.6 on a different machine, it’s ok.
> 
> The internal representation is wrong.
> 
> Will upgrading ruby fix this problem?

I suspect so:  
christopher@ubuntu:~$ ruby -v -rbigdecimal -e  
‘d=BigDecimal.new(“1.0234”); p d; p d.to\_f’  
ruby 1.8.6 (2009-08-04 patchlevel 383) [x86\_64-linux]  
#BigDecimal:7ff22e270380,‘0.10234E1’,18(18)  
1.0234
