# Floating point accuracy in JRuby

**URL:** <https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557>\
**Category:** Ruby\
**Created:** [June 18, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557 "2009-06-18T21:45:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [June 18, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557/1 "2009-06-18T21:45:51Z")

</div>

[03:23 PM (5) omega:thoth ~] \> jirb  
irb(main):001:0\> 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1  
=\> 0.9999999999999999  
[03:33 PM (8) omega:thoth ~] \> irb

> > 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1  
> > =\> 1.0

Disappointing that JRuby does not handle floating point numbers (a) in  
the the same way as CRuby and (b) not as accurately. Is the cost of  
implementing CRuby’s behavior prohibitively high on the JVM, is there  
a fix planned in the future? How would JRuby users recommend handling  
currency on that platform?

steven

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [June 18, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557/2 "2009-06-18T23:02:05Z")

</div>

Steven D. Arnold wrote:

> fix planned in the future? How would JRuby users recommend handling  
> currency on that platform?
> 
> steven

irb(main):001:0\> printf “%20.18f”,  
0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1  
0.999999999999999889=\> nil

irb(main):004:0\* RUBY\_VERSION  
=\> “1.8.6”

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [June 18, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557/3 "2009-06-18T23:13:13Z")

</div>

When handling currency I’d suggest storing everything in cents with  
integers  
and using a Money library like [http://money.rubyforge.org/](http://money.rubyforge.org/). You won’t  
have  
any rounding errors then.

On Thu, Jun 18, 2009 at 3:44 PM, Steven D. Arnold \<

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [June 18, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/floating-point-accuracy-in-jruby/170557/4 "2009-06-18T23:16:44Z")

</div>

Steven D. Arnold wrote:

> fix planned in the future? How would JRuby users recommend handling  
> currency on that platform?
> 
> steven

I don’t think it’s a good idea to use binary floating-point numbers for  
currency in any language or any platform. See the numerous previous  
discussions of binary vs. decimal arithmetic on this list.

Use integer cents instead, or BigDecimal.
