简体   繁体   中英

Storing decimal number with MySQL

What's the best type to store values such:

48.89384 and -2.34910

Actually I'm using float .

Use decimal for exact values.

Notes:

  • ABS (Latitude) <= 90
  • ABS (Longitude) <= 180

So you can us 2 different types

  • Latitude = decimal (x+2, x)
  • Longitude = decimal (y+3, y)

x and y will be the desired precision. Given a metre is 1/40,000,000 of the earth's circumferemce, something like 6-8 will be enough depending on whether you're going for street or full stop accuracy in location.

如果您想要精确表示,并且知道适用的比例,则可以使用十进制数据类型

If you work with money use DECIMAL type. It has no floating-points inaccuracy.

@MiniNamin
if you are using sql then it will also work by putting the DataType Numeric(18,4)

The benefit of floating point is that it can scale from very small to very large numbers. The cost of this, however, is that you can encounter rounding errors.

In the case that you know exactly what level of accuracy you need and will work within, the numeric / decimal types made available to you are often more appropriate. While working within the level of accuracy you specifify on creation, they will not encounter any rounding errors.

That depends on what you're using the numbers for. If these numbers are latitude and longitude, and if you don't need exact representations, then FLOAT will work. Use DOUBLE PRECISION for more accuracy.

But for exact representations, use DECIMAL or NUMERIC . Or INT or one of its different sizes, if you'll never have fractions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM