PostgreSQL CEIL and FLOOR Functions (5 Examples)

Need to round a price up to the next whole number? PostgreSQL CEIL does it: ceil(24.211) returns 25. FLOOR goes the other way and returns 24.

The catch is integer division. ceil(7/2) returns 3, not 4, because PostgreSQL turns 7/2 into 3 before CEIL runs.

The five examples cover return types, boxes, cents, the nearest 5 or 10, and negatives. Tested on PostgreSQL 18.6 for Windows, via psql in VS Code.

PostgreSQL CEIL and FLOOR Function Syntax

Here is the syntax:

ceil ( numeric ) → numeric
ceil ( double precision ) → double precision
floor ( numeric ) → numeric
floor ( double precision ) → double precision

CEILING is an alias for CEIL. You pass one number; an integer becomes double precision.

Return value: the nearest whole number up (CEIL) or down (FLOOR), in the input’s type. NULL returns NULL.

Availability: every supported PostgreSQL release, including 18.

When to Use the CEIL Function in PostgreSQL

Use them to:

  • count boxes or pages for an order
  • bill every started hour
  • put prices and weights into brackets

Unsure of a column’s type? Check PostgreSQL data types.

Run this once; every example uses this orders table:

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
  order_id  int PRIMARY KEY,
  customer  text,
  units     int,
  minutes   int,
  price     numeric(10,3),
  weight_lb double precision
);
INSERT INTO orders VALUES
 (1001, 'Hannah Brooks',  7,  95, 24.211, 12.4),
 (1002, 'Tyler Reed',    12,  60, 18.005, 3.75),
 (1003, 'Grace Miller',  25, 121,  7.499, 21.0);

Example 1: Round Up and Down to a Whole Number

Let’s start with Hannah Brooks’ order. I want both directions plus each result’s type:

SELECT price,
       ceil(price)  AS up,
       floor(price) AS down,
       pg_typeof(ceil(price))     AS price_type,
       ceil(weight_lb)            AS weight_up,
       pg_typeof(ceil(weight_lb)) AS weight_type
FROM orders
WHERE order_id = 1001;

Output:

 price  | up | down | price_type | weight_up |   weight_type    
--------+----+------+------------+-----------+------------------
 24.211 | 25 |   24 | numeric    |        13 | double precision
(1 row)
PostgreSQL ceil(24.211) returning 25 numeric and ceil(12.4) returning 13 double precision
CEIL keeps the input type.

For order 1001, this returns 25 and 24 for the price and 13 for the weight.

Numeric in gives numeric out. The scale goes too: ceil(5.0) returned 5.

Example 2: Calculate Boxes Needed and Billed Hours with CEIL

Now the catch from the intro. A box holds 6 units. The goal is boxes and billed hours per order:

SELECT units,
       ceil(units / 6)      AS wrong_boxes,
       ceil(units / 6.0)    AS boxes,
       ceil(minutes / 60.0) AS billed_hours
FROM orders;

Output:

 units | wrong_boxes | boxes | billed_hours 
-------+-------------+-------+--------------
     7 |           1 |     2 |            2
    12 |           2 |     2 |            1
    25 |           4 |     5 |            3
(3 rows)
PostgreSQL ceil(units / 6) returning 1 versus ceil(units / 6.0) returning 2 boxes for 7 units
Integer division runs first.

Seven units need 2 boxes, and 95 minutes bill as 2 hours. The wrong_boxes column says 1.

units / 6 is integer division, so PostgreSQL throws the fraction away before CEIL ever sees it. My PostgreSQL CAST tutorial shows the ::numeric fix.

Example 3: Round Up or Down to 2 Decimal Places in PostgreSQL

CEIL has no decimals argument. Say you need cents. Multiply by 100, round, divide:

SELECT price,
       ceil(price * 100) / 100 AS up_raw,
       (ceil(price * 100) / 100)::numeric(10,2)  AS up_cents,
       (floor(price * 100) / 100)::numeric(10,2) AS down_cents
FROM orders
WHERE order_id IN (1001, 1002);

Output:

 price  |       up_raw        | up_cents | down_cents 
--------+---------------------+----------+------------
 24.211 | 24.2200000000000000 |    24.22 |      24.21
 18.005 | 18.0100000000000000 |    18.01 |      18.00
(2 rows)
PostgreSQL ceil(price * 100) / 100 returning 24.2200000000000000, then 24.22 after the numeric(10,2) cast
The cast trims the zeros.

For 24.211, this returns 24.22 up and 24.21 down. Tyler Reed’s 18.005 gives 18.01 and 18.00.

Numeric division adds 16 decimals, hence the cast. To total them, use PostgreSQL SUM.

Run the SELECT again and you should see two decimals in both cents columns.

Example 4: Round to the Nearest 5 or 10

You want 5 lb shipping tiers and $10 price brackets. Divide by the step, round, multiply back:

SELECT weight_lb,
       ceil(weight_lb / 5) * 5 AS tier_lb,
       floor(price / 10) * 10  AS bracket
FROM orders;

Output:

 weight_lb | tier_lb | bracket 
-----------+---------+---------
      12.4 |      15 |      20
      3.75 |       5 |      10
        21 |      25 |       0
(3 rows)

Hannah’s 12.4 lb lands in the 15 lb tier and her $24.211 in the $20 bracket. Grace Miller’s $7.499 gives 0.

To total each tier, group by the expression; see PostgreSQL GROUP BY.

Example 5: CEIL and FLOOR with Negative Numbers and NULL

Last, edge cases. I want 2.5, -2.5 and NULL through four functions:

\pset null '(null)'
SELECT v, ceil(v), floor(v), round(v), trunc(v)
FROM (VALUES (2.5), (-2.5), (NULL)) AS t(v);

Output:

   v    |  ceil  | floor  | round  | trunc  
--------+--------+--------+--------+--------
    2.5 |      3 |      2 |      3 |      2
   -2.5 |     -2 |     -3 |     -3 |     -2
 (null) | (null) | (null) | (null) | (null)
(3 rows)
PostgreSQL ceil(-2.5) returning -2 and floor(-2.5) returning -3 next to round and trunc
CEIL moves up.

For -2.5, CEIL returns -2 and FLOOR returns -3. NULL stays NULL.

CEIL always moves up the number line. Wrap the input in COALESCE if NULL should count as 0.

What Is the Difference Between CEIL, FLOOR, ROUND and TRUNC?

FunctionDirection-2.5
CEILUp-2
FLOORDown-3
ROUNDNearest-3
TRUNCToward zero-2

My rule of thumb: CEIL for counts you can’t split, FLOOR for finished units, and PostgreSQL ROUND for the nearest value.

Tips & Common Mistakes

  • Divide by a decimal: ceil(7/2.0) returned 4.
  • An integer input returns double precision, not integer.
  • Text fails: ERROR: invalid input syntax for type double precision: "abc".
  • ceil(-0.4::float8) prints -0; numeric prints 0.

Wrapping Up

PostgreSQL CEIL rounds up and FLOOR rounds down. Copy Example 2 for counts and Example 3 for cents.

Next step: browse the complete list of PostgreSQL functions, or the PostgreSQL 18 math functions page.

More PostgreSQL tutorials:

Frequently Asked Questions

How do I round up integer division in PostgreSQL without CEIL?

Use (a + b – 1) / b. (7 + 6 – 1) / 6 returned 2 as an integer.

Can I use CEIL on a timestamp in PostgreSQL?

No, it errors. Use date_trunc(‘hour’, ts) + interval ‘1 hour’; 09:20 became 10:00.

How do I get an integer from CEIL in PostgreSQL?

Cast it: ceil(price)::int returned 25 as integer.