Bug in JRuby BigDecimal.to_s when amount is in cents

Posted by Ben on December 06, 2007

We found an interesting bug yesterday while working on one of our JRuby projects. In our application we are downloading transactions from various banks. I noticed an interesting bug while testing against my bank account where transactions with only cents and no whole amount were downloaded incorrectly. A transaction for $0.45 would, incorrectly be downloaded as $4.50. We determined that the issue stemmed from a JRuby bug in BigDecimal. The following test cases showcase the bug.

BigDecimal.new("0.10").to_s("F")
=> "1.0"
BigDecimal.new("0.01").to_s("F")
=> "1.0"

I have created a patch for JRuby 1.0.2 which fixes the issue. I have submitted to the JRuby project and Headius told me it should make it into JRuby 1.0.3 and also the trunk. If you can’t wait for 1.0.3, which is due in the next couple of weeks, you can download the patch I submitted. If you are too lazy to apply the patch your self, let me know and I’ll send you the JRuby jar file with the applied patch that I created for our project.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Steven Herod Thu, 13 Dec 2007 04:34:19 PST

    I just spent a few hours with this one thinking I was doing something stupid before I thought…. hmmm… maybe its a Jruby bug.

    And sure enough, found your post. Thanks for confirming it!

Comments