List the round function in postgres:
\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
The argument for round must be double precision,numeric,integer.
select 6::float/3.3 as number;
number
--------------------
1.8181818181818183
(1 row)
If its data type is double precision :
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
Which data type is the result of division—-1.8181818181818183?
>Solution :
You can always check things like that with pg_typeof(): demo
select pg_typeof(6::float/3.3) as number_type;
| number_type |
|---|
| double precision |
The reason your function call didn’t work is that \df showed you three different round() functions:
round ( numeric ) → numeric
round ( double precision ) → double precisionRounds to nearest integer. For
numeric, ties are broken by rounding away from zero. Fordouble precision, the tie-breaking behavior is platform dependent, but “round to nearest even” is the most common rule.
and only the one accepting numeric has a variant with a 2nd argument, integer, to specify how
many decimal places you want to round to.
round ( v numeric,s integer) → numericRounds
vtosdecimal places. Ties are broken by rounding away from zero.
So if you cast to ::numeric instead or don’t cast all and let PostgreSQL assume numeric by default, you can get that one to get picked:
select round(6/3.3,4) as number;
| number |
|---|
| 1.8182 |