Skip to content

Instantly share code, notes, and snippets.

@metaskills
Last active December 15, 2015 16:38
Show Gist options
  • Save metaskills/4630660 to your computer and use it in GitHub Desktop.
Save metaskills/4630660 to your computer and use it in GitHub Desktop.
# Odd behavior in BigDecimal's util for Float#to_d
# on Ruby 1.9.3 and 2.0?
#
require 'bigdecimal'
require 'bigdecimal/util'
puts "85.26.to_d.to_s('F') # => #{85.26.to_d.to_s('F')}"
puts "BigDecimal.new('85.26').to_s('F') # => #{BigDecimal.new('85.26').to_s('F')}"
oddities = ('0.00'..'99.99').to_a.select do |v|
v.to_f.to_d.to_s('F') =~ /\.\d{3}/
end
puts "Found #{oddities.size} oddities:"
puts "--------------------------------"
oddities.each do |v|
puts "Float: #{v} #to_d: #{v.to_f.to_d.to_s('F')}"
end
@johnbhall
Copy link

Thanks Ken! Very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment