Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which data type is the result of division?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 precision

Rounds to nearest integer. For numeric, ties are broken by rounding away from zero. For double 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 ) → numeric

Rounds v to s decimal 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading