列出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来判断这样的事情

select pg_typeof(6::float/3.3) as number_type;
number_type
double precision

您的函数调用不起作用的原因是\df向您显示了three different round() functions.前两个恰好接受one个参数:

round ( numeric ) → numeric

round ( double precision ) → double precision

四舍五入为最接近的整数.对于numeric,通过从零四舍五入来打破平局.对于double precision来说,打破平局的行为取决于平台,但"舍入到最接近的偶数"是最常见的规则.

并且只有接受numeric的一个变量具有第二个参数integer,以指定如何 你想四舍五入到小数点后几位.

round ( v numeric, 101 ) → numeric

将小数点后v位舍入到s位.通过舍入零来打破平局.

因此,如果你转换为::numeric,或者根本不转换,而让PostgreSQL presume numeric by default,你可以让那个被选中:

select round(6/3.3,4) as number;
number
1.8182

混合类型数学运算中的结果类型为Here's the doc:

涉及多个参数数据类型(如integer+numeric)的调用将使用这些列表中后面出现的类型进行解析.

正在讨论的名单是"100, 101, 102, 103, 104, and 105".这里有一张小抄:

divident /smallint divisor /integer divisor /bigint divisor /numeric divisor /real divisor /double precision divisor
smallint smallint integer bigint numeric double precision double precision
integer integer integer bigint numeric double precision double precision
bigint bigint bigint bigint numeric double precision double precision
numeric numeric numeric numeric numeric double precision double precision
real double precision double precision double precision double precision real double precision
double precision double precision double precision double precision double precision double precision double precision

Postgresql相关问答推荐

函数返回查询执行plpgsql时不存在列

PostgreSQL中btree_gist索引涉及integer和tstzrange属性查询计划的问题

Postgr不列出运算符和函数

Select 所有数组类型和维度

正在应用序列化迁移,但数据库没有更改

正在加载 pgAdmin 4 v7.4...同时打开 pgAdmin

postgres hierarchy - 用祖先的值填充缺失值

postgres 中的模式前缀,被调用元素的范围

使用 postgresql Select 整数作为位和状态表

PostgreSQL unnest 与空数组

如何在 PSQL 中查找从另一个表继承的子表

postgresql:致命:password authentication failed for user "douglas"

NOT LIKE 带有 NULL 值的行为

如何在 postgres 模式中列出关系

安装时 postgres 的默认用户

从 PostgreSQL 序列中 Select 多个 id

PostgreSQL 获取过go 12 小时的元素

PostgreSQL:如果不存在则创建表 AS

我应该在 Django DATABASE ENGINE 中使用哪个 Postgres 值?

PostgreSQL - 如何在不同时区呈现日期?