# What are FLOOR and CEILIING in PostGIS designated for?

Just a short question to lighten up my understanding of some PostGIS-code:

Regarding the creation of user-defined grids I often find the expressions CEILING and FLOOR. Like in this non-representative example:

SELECT floor(st_xmax(the_polygon)/1500) FROM ...

What are they exactly designated for?

Thanks in advance for lightening me up!

knutella

Floor and ceiling are classical computer and math functions that basically round numbers down or up.

``````select x, floor(x), ceil(x), trunc(x)
from (select unnest(ARRAY[-4.8, -4.2, 4.2, 4.8]) as x) as f;

x   | floor | ceil | trunc
------+-------+------+-------
-4.8 |    -5 |   -4 |    -4
-4.2 |    -5 |   -4 |    -4
4.2 |     4 |    5 |     4
4.8 |     4 |    5 |     4
(4 rows)
``````

In the context of building a grid, it can be used to define the extents of the grid to aesthetically-pleasing numbers. For example, if the x- extents of a layer go between Eastings 2754324.254 and 2770756.610, they can be "rounded" to the bounding 1 km grid with:

``````select floor(xmin/1000) * 1000 as xmin, ceil(xmax/1000) * 1000 as xmax
from (select 2754324.254 as xmin, 2770756.610 as xmax) as f;

xmin   |  xmax
---------+---------
2754000 | 2771000
(1 row)
``````
• Hello Mike, thanks for the extensive answer! It makes sense now in the context, that I’m using it. I guess my main mistake was, to think FLOOR and CEILINING are special PostGIS-functions. That was also the reason, that Google didn't told me smth useful. If I look for it in connection with PostgreSQL the results are 'slightly' different.. %) Nevertheless thanks a lot for explaining it directly to the grid-manner=) – knutella Jan 21 '13 at 14:53