0

I have a column latitude of type real in my Postgres table.

Looking at it in Postico, it has the value of 57.70887. But selecting it from psql or via code, the value returned is rounded to 4 decimal places:

# SELECT latitude from categories where id = 4;

 latitude
----------
  57.7089
(1 row)

What am I doing wrong?

Postgres 9.6.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
  • 1
    [extra_float_digits](https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS) –  Mar 16 '18 at 09:52
  • @a_horse_with_no_name Thank you, that is correct. `SET extra_float_digits = 3;` ➜ `select ..` ➜ `57.7088699`. I'll accept it if you post it as an answer :) – Markus Hedlund Mar 16 '18 at 10:18
  • 2
    @Znarkus just a off topic hint: if you're storing latitudes, I assume you're dealing with longitudes too ;-) so I would really recommend you to use the `postgis` extension for storing points. I might seem in the beginning a bit unnecessary, but when spatial queries are involved, it can absolutely save you a lot of trouble. Cheers :-) – Jim Jones Mar 16 '18 at 11:25

1 Answers1

0

The column should not be real if you care for precision. Floating point types only store approximate values. Use numeric instead to preserve the value exactly as inserted.

Consider:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228