I have a model that uses floating points to store geolocations. It stores the data in MySQL, using ActiveRecord, Rails 3.2.x.
c = Camping.new()
c.latitude=51.77802459999999
c.save
c.latitude
=> 51.77802459999999
c.reload
c.latitude
=> 51.778
When looking in the database, I confirm it is stored as 51.778. Other numbers are stored with more precision, I have the idea the 0
is what makes either ActiveRecord or MySQL decide to omit the rest.
What decides that the number could or should be chopped off after three digits? Is there anything to control this? Is float the correct type to store this in?
I want a precision of 6, never more, but could be less, or padded. So when 51.7780
is provided, it might be stored as 51.7780
or as 51.778000
. When 51.77802459999999
is provided, it could be stored as 51.778025
or as the number with full precision; I don't really care.
Relevant part from schema.rb
:
create_table "campings", :force => true do |t|
#...
t.float "latitude"
t.float "longitude"
end