Bug #3226: BigDecimal: incorrectly parses "1.0x" as "1.x", and similar
http://redmine.ruby-lang.org/issues/show/3226

Author: A Nonymous
Status: Open, Priority: High
Category: core, Target version: Ruby 1.8.7
ruby -v: ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

This problem is in the version of ruby which ships with Mac OS X 10.6, it may also exist in other versions.
BigDecimal does not parse decimals correctly when the fractional portion begins with a zero.

irb(main):001:0>  BigDecimal.new("1.01")
=> #<BigDecimal:1011775d0,'0.11E1',8(8)>
irb(main):002:0> BigDecimal.new("1.01").to_f
=> 1.1
     ^----- WHAT? THAT'S NOT RIGHT!

irb(main):003:0> BigDecimal.new("1.001")
=> #<BigDecimal:101166b18,'0.11E1',8(8)>
irb(main):004:0> BigDecimal.new("1.001").to_f
=> 1.1
     ^----- WHAT? THAT'S NOT RIGHT!

irb(main):005:0> BigDecimal.new("1.0001")
=> #<BigDecimal:101155ca0,'0.11E1',8(12)>
irb(main):006:0> BigDecimal.new("1.0001").to_f
=> 1.1
     ^----- WHAT? THAT'S NOT RIGHT!

irb(main):007:0> BigDecimal.new("123.0456")
=> #<BigDecimal:10113b008,'0.123456E3',8(12)>
irb(main):008:0> BigDecimal.new("123.0456").to_f
=> 123.456
       ^----- WHAT? THAT'S NOT RIGHT!

irb(main):009:0> BigDecimal.new("123.00456")
=> #<BigDecimal:10110b128,'0.123456E3',12(12)>
irb(main):010:0> BigDecimal.new("123.00456").to_f
=> 123.456
       ^----- WHAT? THAT'S NOT RIGHT!

irb(main):011:0> BigDecimal.new("123.000456")
=> #<BigDecimal:10062a470,'0.123456E3',12(16)>
irb(main):012:0> BigDecimal.new("123.000456").to_f
=> 123.456
       ^----- WHAT? THAT'S NOT RIGHT!

This problem turned up when doing arithmetic on numbers stored as MySQL DECIMAL columns using ActiveRecord, as ActiveRecord parses those columns into BigDecimals.


----------------------------------------
http://redmine.ruby-lang.org