Skip to content

Instantly share code, notes, and snippets.

@mattsoutherden
Created September 7, 2011 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsoutherden/1200438 to your computer and use it in GitHub Desktop.
Save mattsoutherden/1200438 to your computer and use it in GitHub Desktop.
Strange Ruby Behaviour
>> d.wordpress_deal.merchant_value
=> 37.05
>> d.wordpress_deal.merchant_value * 100
=> 3705.0
>> (d.wordpress_deal.merchant_value * 100).to_i
=> 3704
>>
@levent
Copy link

levent commented Sep 7, 2011

don't ask

require 'bigdecimal'
(BigDecimal('37.05') * BigDecimal('100.0')).to_f
=> 3705.0

see http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

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