Ruby on Rails Column

If you specify that the ActiveRecord column type is decimal, Rails will return that database value as a Ruby BigDecimal object.

My question is that when working with these fields and doing extra math on them, I should always use BigDecimal values ​​in my calculations or use floating point values ​​in order. I did not know if it is good practice to mix BigDecimal values ​​and Floating Point values ​​in the same calculations.

Mixed example: BigDecimal.new ('12 .43 ') /4.2

Same type Example: BigDecimal.new ('12 .43 ') /BigDecimal.new (' 4.2 ')

The reason I'm asking for is because I'm a little scared using floats because I need decimal precision. 0.1 + 0.7 will not equal 0.8 using floats.

+6
ruby-on-rails bigdecimal
source share
1 answer

Since you

... a little gun shy from using floats ...

use BigDecimal. Works great

+2
source share

All Articles