列出 postgres 中的 round 函数:
\df round
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+-------+------------------+---------------------+------
pg_catalog | round | double precision | double precision | func
pg_catalog | round | numeric | numeric | func
pg_catalog | round | numeric | numeric, integer | func
round 的参数必须是双精度、数字、整数。
select 6::float/3.3 as number;
number
--------------------
1.8181818181818183
(1 row)
如果其数据类型是双精度:
select round(6::float/3.3,4) as number;
ERROR: function round(double precision, integer) does not exist
LINE 1: select round(6::float/3.3,4) as number;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Time: 0.517 ms
除法的结果是哪种数据类型---- 1.8181818181818183
?
您可以随时使用以下命令检查类似内容
pg_typeof()
:demo您的函数调用不起作用的原因是它向
\df
您显示了三个不同的round()
函数。前两个只接受一个参数:并且只有接受的那个
numeric
有一个带有第二个参数的变体integer
,用于指定要四舍五入到的小数位数。因此,如果您改为强制转换
::numeric
或根本不强制转换并让 PostgreSQL默认进行numeric
假定,则可以选择该选项:这是关于混合类型数学运算中结果类型的文档:
有问题的列表是“
smallint
、、、、、和”integer
bigint
numeric
real
double precision
。这是一个备忘单: