As stated in the documentation of Postgres 9.0 the double precision
data type has a precision of 15 decimal digits and a storage of 8 bytes, then an integer number larger than a normal bigint
(8 bytes) stored in a double precision
field is approximated. Correct me if I'm wrong, I say larger than a normal bigint
because if you try to cast this number to bigint
you get this error:
select 211116514527303268704::bigint;
>> ERROR: bigint out of range
When you try to convert this to double precision
and numeric
and compare both you find that they're the same:
select 211116514527303268704::numeric,
211116514527303268704::double precision,
(211116514527303268704::double precision) = (211116514527303268704::numeric);
+-----------------------+----------------------+---------+
| numeric | float8 | boolean |
+-----------------------+----------------------+---------+
| 211116514527303268704 | 2.11116514527303e+20 | t |
+-----------------------+----------------------+---------+
and with the to_char()
function they return different values:
select
trim(to_char((211116514527303268704::double precision),'999,999,999,999,999,999,999')),
trim(to_char((211116514527303268704::numeric),'999,999,999,999,999,999,999'));
+-----------------------------+-----------------------------+
| text | text |
+-----------------------------+-----------------------------+
| 211,116,514,527,303,270,400 | 211,116,514,527,303,268,704 |
+-----------------------------+-----------------------------+
As you can see the value returned with the to_char - numeric combination is correct but the to_char - double precision loses consistency from the exponential part in double precision
2.11116514527303e+20
I'm not sure if it affect something but the locale of lc_numeric
is 'es_PY.utf8'
It's completely useless to implement double precision
in this case or it's another alternative to keep those double precision fields? That is always the preferred option, there's some type of cast to numeric
from double precision
that keeps all the original digits?
For additional information I have a PostgreSQL 9.0 installation running in a CentOS 6 x86-64 server.