【发布时间】:2015-05-28 16:48:35
【问题描述】:
正如documentation of Postgres 9.0 中所述,double precision 数据类型具有 15 个十进制数字的精度和 8 个字节的存储空间,然后是一个大于普通 bigint(8 个字节)的整数,存储在 @987654324 中@字段是近似的。如果我错了,请纠正我,我说大于正常的 bigint,因为如果您尝试将此数字转换为 bigint,则会收到此错误:
select 211116514527303268704::bigint;
>> ERROR: bigint out of range
当您尝试将其转换为 double precision 和 numeric 并比较两者时,您会发现它们是相同的:
select 211116514527303268704::numeric,
211116514527303268704::double precision,
(211116514527303268704::double precision) = (211116514527303268704::numeric);
+-----------------------+----------------------+---------+
| numeric | float8 | boolean |
+-----------------------+----------------------+---------+
| 211116514527303268704 | 2.11116514527303e+20 | t |
+-----------------------+----------------------+---------+
使用to_char() 函数,它们返回不同的值:
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 |
+-----------------------------+-----------------------------+
如您所见,使用 to_char - 数字组合返回的值是正确的,但 to_char - 双精度与 double precision 2.11116514527303e+20 中的指数部分失去一致性
我不确定它是否会影响某些东西,但 lc_numeric 的语言环境是 'es_PY.utf8'
在这种情况下实现double precision 完全没用,还是保留这些双精度字段的另一种选择?这始终是首选选项,有某种类型的从 double precision 到 numeric 的转换可以保留所有原始数字?
有关更多信息,我在 CentOS 6 x86-64 服务器上运行了 PostgreSQL 9.0 安装。
【问题讨论】:
标签: postgresql double precision numeric to-char