简体   繁体   English

为什么我的PostgreSQL“真实”数据被四舍五入?

[英]Why are my PostgreSQL “real” data being rounded?

I have a table with a column named total having the real data type. 我有一个表,其中有一个名为total的列,具有real数据类型。

But I'm finding that this column is being rounded when I query its value, as illustrated in the following: 但是我发现在查询其值时该列已被四舍五入,如下所示:

SELECT total FROM data WHERE id='xe54g';
 total 
---------
  105861

UPDATE data SET total=105860.5 WHERE id='xe54g';        
UPDATE 1

SELECT total FROM data WHERE id='xe54g';
 total 
---------
  105860

UPDATE data SET total=105860.7 WHERE id='xe54g';        
UPDATE 1

SELECT total FROM data WHERE id='xe54g';
 total 
---------
  105861

\d+ data
   Column  |  Type | Collation | Nullable |  Default  | Storage  | Stats target | Description 
-----------+-------+-----------+----------+-----------+----------+--------------+-------------
 total     | real  |           |          | 0         | plain    |              | 

How do I get it to return the full precision? 如何获得返回的全精度? I'm using PG 11.1. 我正在使用PG 11.1。

You can get the full precision by setting 您可以通过设置获得全精度

extra_float_digits = 3

Then PostgreSQL will also display decimal digits that may contain rounding errors. 然后PostgreSQL也将显示可能包含舍入错误的十进制数字。

Note that real has a low number of significant digits. 请注意, real数的有效位数很少。 You'd be happier if you used double precision . 如果使用double precision会更快乐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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