Rounding
Core idea
ROUND changes a numeric result to a requested number of decimal places. FLOOR moves toward negative infinity, CEIL (or CEILING) toward positive infinity, and TRUNC toward zero. Choose the rule that matches the business requirement rather than assuming they are interchangeable.
Minimal example
SQLite 3.46.1 example.
SELECT ROUND(2.5), ROUND(-2.5), FLOOR(-2.3), CEIL(-2.3), TRUNC(-2.3);Observed rows (JSON notation; null means SQL NULL):
[ [ 3, -3, -3, -2, -2 ]]How it works
Use ROUND(value, digits), FLOOR(value), CEIL(value), TRUNC(value). For -2.3, the floor is -3, the ceiling is -2 and truncation is -2. SQLite ROUND returns a floating-point value; the two-argument PostgreSQL ROUND accepts numeric, not double precision. PostgreSQL numeric rounds ties away from zero; floating-point tie handling is platform-dependent. A rounded value is not a fixed-width display string. For example, 150 cents corresponds to 1.50 units, but a client may display 1.5.
Practical example
SQLite 3.46.1 example. Load the V22 shop-setup.sql fixture first.
SELECT id, ROUND(price_cents / 100.0, 2) AS unit_priceFROM products ORDER BY id;Observed rows (JSON notation; null means SQL NULL):
[ [ 1, 3.5 ], [ 2, 1.5 ], [ 3, 24 ], [ 4, 1 ]]Common mistake and practice
Do not round every intermediate result unless the accounting rule requires it. For the shop fixture, predict the displayed unit price of each product when rounded to one decimal place; change the second ROUND argument to 1 and compare. Use integer cents or an appropriate exact decimal type for monetary storage, and format separately for display.