Float vs Decimal in ActiveRecord

Sometimes, Activerecord data types confuse me. Err, often. One of my eternal questions is, for a given case,

Should I use :decimal or :float?

I’ve often come across this link, ActiveRecord: :decimal vs :float?, but the answers aren’t quite clear enough for me to be certain:

I’ve seen many threads where people recommend flat out to never use
float and always use decimal. I’ve also seen suggestions by some
people to use float for scientific applications only.

Here are some example cases:

  • Geolocation/latitude/longitude: -45.756688, 120.5777777, …
  • Ratio/percentage: 0.9, 1.25, 1.333, 1.4143, …

I have used :decimal in the past, but I found dealing with BigDecimal objects in Ruby was unnecessarily awkward as compared to a float. I also know I can use :integer to represent money/cents, for example, but it doesn’t quite fit for other cases, for example when quantities in which precision could change over time.

  • What are the advantages/disadvantages of using each?
  • What would be some good rules of thumb to know which type to use?

3 Answers
3

Leave a Comment