Last active
December 15, 2015 16:38
-
-
Save metaskills/4630660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Klaus! I think I may spend some time in ActiveRecord so that
value_to_decimal(value)
takes a precision so that column reflection can pass down the proper information.