Issue Details (XML | Word | Printable)

Key: JRUBY-1151
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Charles Oliver Nutter
Reporter: Nils Olryd
Votes: 0
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
JRuby

BigDecimal to_s displays incorrect number when number has no whole part

Created: 17/Jun/07 03:02 AM   Updated: 23/Apr/08 10:02 AM
Component/s: Core Classes/Modules
Affects Version/s: JRuby 1.0.0
Fix Version/s: JRuby 1.1RC2

Time Tracking:
Not Specified

File Attachments: 1. Text File big_decimal_bug_fix2.patch (2 kB)
2. Text File bigDecimal.patch (9 kB)
3. Java Source File RubyBigDecimal.java (28 kB)
4. File testBigDecimal.rb (4 kB)


Testcase included: yes
Patch Submitted: Yes


 Description  « Hide
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.

 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Drew added a comment - 08/Oct/07 08:20 PM
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?


Charles Oliver Nutter added a comment - 22/Oct/07 12:37 AM
Already fixed at some point, and no follow-up from the reporter.

Charles Oliver Nutter added a comment - 06/Dec/07 12:30 AM
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.

Ben Curren added a comment - 06/Dec/07 12:33 AM
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.

Ben Curren added a comment - 07/Dec/07 08:40 PM
Fixed another bug in the to_s("F") method that were introduced with my last fix.

Charles Oliver Nutter added a comment - 12/Jan/08 12:53 AM
This needs to go in the next releases (1.0.4 and 1.1RC2)

Charles Oliver Nutter added a comment - 15/Jan/08 04:01 PM
Patch does not apply cleanly to trunk, so we'll have to smith it a little.

Robert Jay Osborne added a comment - 17/Jan/08 08:38 AM
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.

Charles Oliver Nutter added a comment - 26/Jan/08 02:09 AM
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!