When saving my longtitude and latitude in my Rails 3.2 app, the value’s getting truncated on save.
I’ve tried in the console and it’s saving the full value:
item.update_attributes(:latitude => '51.07763839854951')
item.latitude:
=> 51.07763839854951
Saving the same value in the browser gives me an output of:
51.0865174
What can I do to prevent this?
The value is probably being truncated when it is stored in the DB. Regardless of the exact cause, if this value is precise and needs to be stored and retrieved losslessly, a floating-point number is probably the wrong data type to use. Floating point numbers can lose precision when you perform certain arithmetic operations on them, so they are not appropriate for values which must be exact.
When you are defining your DB schema using Rails migrations, you can use the
:decimaltype for decimal values which must be stored precisely. (When ActiveRecord pulls these values out of the DB, they will becomeBigDecimalobjects rather thanFloats You can do arithmetic onBigDecimals without losing precision.)