Rails 3 Float or decimal for GPS coordinates

前端 未结 4 1176
走了就别回头了
走了就别回头了 2020-12-31 09:00

I need to store GPS coordinates in a database. I\'ve heard that floats are less accurate than decimals. Is that true? If so, what reason is there to ever use floats?

4条回答
  •  感动是毒
    2020-12-31 09:53

    If you want more accurate GPS coordinates, then yes decimals are the way to go. You can create them with a migration like:

    create_table "models" do |t|
      t.decimal  "latitude", :precision => 15, :scale => 10, :default => 0.0
      t.decimal  "longitude", :precision => 15, :scale => 10, :default => 0.0
    end
    

    The reason people use floats is that they are usually precise enough for most use cases and use less space to store.

提交回复
热议问题