JRuby

BigDecimal to_s displays incorrect number when number has no whole part

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: JRuby 1.0.0
  • Fix Version/s: JRuby 1.1RC2
  • Component/s: Core Classes/Modules
  • Labels:
    None
  • Testcase included:
    yes
  • Patch Submitted:
    Yes
  • Number of attachments :
    4

Description

The statement puts BigDecimal("0.23") prints "2.3". This is due to the way the whole and fraction part are constructed in to_s, based on the unscaled value. The unscaled value should have a leading 0 in order for the to_s function to work correctly. Included is the RubyBigDecimal.java file with a simple modification. Also included is the testBigDecimal.rb file with cases added for positive and negative numbers with 0 wholes.

  1. big_decimal_bug_fix2.patch
    07/Dec/07 8:40 PM
    2 kB
    Ben Curren
  2. bigDecimal.patch
    17/Jan/08 8:38 AM
    9 kB
    Robert Jay Osborne
  3. RubyBigDecimal.java
    17/Jun/07 3:02 AM
    28 kB
    Nils Olryd
  4. testBigDecimal.rb
    17/Jun/07 3:02 AM
    4 kB
    Nils Olryd

Activity

Hide
Drew added a comment -

I attempted to recreate this using the latest version of jruby, and it appears to already have been fixed...

$ jirb
irb(main):001:0> require 'bigdecimal'
=> true
irb(main):002:0> puts BigDecimal("0.23").to_s
0.23E0
=> nil

Can you give a test case where "2.3" will be output?

Show
Drew added a comment - I attempted to recreate this using the latest version of jruby, and it appears to already have been fixed... $ jirb irb(main):001:0> require 'bigdecimal' => true irb(main):002:0> puts BigDecimal("0.23").to_s 0.23E0 => nil Can you give a test case where "2.3" will be output?
Hide
Charles Oliver Nutter added a comment -

Already fixed at some point, and no follow-up from the reporter.

Show
Charles Oliver Nutter added a comment - Already fixed at some point, and no follow-up from the reporter.
Hide
Charles Oliver Nutter added a comment -

Reopening; apparently Rails modifies BigDecimal.to_s to be to_s("F"), which is why we couldn't reproduce this. Patch is coming from a community member.

Show
Charles Oliver Nutter added a comment - Reopening; apparently Rails modifies BigDecimal.to_s to be to_s("F"), which is why we couldn't reproduce this. Patch is coming from a community member.
Hide
Ben Curren added a comment -

I created this patch off of the jruby 1.0.2 tag. This patch should be applied to the trunk as well. Please let me know if you have any questions.

Show
Ben Curren added a comment - I created this patch off of the jruby 1.0.2 tag. This patch should be applied to the trunk as well. Please let me know if you have any questions.
Hide
Ben Curren added a comment -

Fixed another bug in the to_s("F") method that were introduced with my last fix.

Show
Ben Curren added a comment - Fixed another bug in the to_s("F") method that were introduced with my last fix.
Hide
Charles Oliver Nutter added a comment -

This needs to go in the next releases (1.0.4 and 1.1RC2)

Show
Charles Oliver Nutter added a comment - This needs to go in the next releases (1.0.4 and 1.1RC2)
Hide
Charles Oliver Nutter added a comment -

Patch does not apply cleanly to trunk, so we'll have to smith it a little.

Show
Charles Oliver Nutter added a comment - Patch does not apply cleanly to trunk, so we'll have to smith it a little.
Hide
Robert Jay Osborne added a comment -

This should patch cleanly against the trunk. I took the liberty of composing a to_s method out of the spaghetti and leaving engineeringValue and floatingPointValue as the remaining larger methods.

Show
Robert Jay Osborne added a comment - This should patch cleanly against the trunk. I took the liberty of composing a to_s method out of the spaghetti and leaving engineeringValue and floatingPointValue as the remaining larger methods.
Hide
Charles Oliver Nutter added a comment -

I applied this in 5751 along with a simple test:

assert_equal("0.23", BigDecimal.new("0.23").to_s("F))

If you have any additional tests we could add, from what you saw making this fix, please post them. Thank you for your help!

Show
Charles Oliver Nutter added a comment - I applied this in 5751 along with a simple test:
assert_equal("0.23", BigDecimal.new("0.23").to_s("F))
If you have any additional tests we could add, from what you saw making this fix, please post them. Thank you for your help!

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: