Cheerio
Cheerio

Reputation: 1240

Storing decimal number with MySQL

What's the best type to store values such:

48.89384 and -2.34910

Actually I'm using float.

Upvotes: 2

Views: 524

Answers (6)

gbn
gbn

Reputation: 432210

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.

Upvotes: 5

eksortso
eksortso

Reputation: 1301

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.

Upvotes: 0

MatBailie
MatBailie

Reputation: 86706

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.

Upvotes: 0

Dinup Kandel
Dinup Kandel

Reputation: 2505

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

Upvotes: 2

davek
davek

Reputation: 22905

If you want exact representation, and you know the scale that applies, then you can use the decimal data type.

Upvotes: 3

Egor
Egor

Reputation: 120

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

Upvotes: 2

Related Questions