PostgreSQL - rounding floating point numbers

postgresql

Solution

You can accomplish this by doing something along the lines of

select round( (21.04 /0.05 ),0)*0.05

where `21.04` is the number to round and `0.05` is the accuracy.

Problem

I have a newbie question about floating point numbers in PostgreSQL 9.2. Is there a function to round a floating point number directly, i.e. without having to convert the number to a numeric type first? Also, I would like to know whether there is a function to round by an arbitrary unit of measure, such as to nearest 0.05? When casting the number into a decimal form first, the following query works perfectly: ``` SELECT round(1/3.::numeric,4); round -------- 0.3333 (1 row) Time: 0.917 ms ``` However, what really I'd like to achieve is something like the following: ``` SELECT round(1/3.::float,4); ``` which currently gives me the following error: ``` ERROR: function round(double precision, integer) does not exist at character 8 Time: 0.949 ms ``` Thanks

Original source

Related problems