NUMERIC/DECIMAL
numeric stores exact decimal numbers, which makes it the right choice for money.
In this page:
Syntax
column_name numeric(precision, scale)
column_name numeric(10, 2)
NUMERIC/DECIMAL
numeric(precision, scale) keeps exact digits: numeric(10,2) holds up to 10 digits with 2 after the decimal point. real and double precision are binary floating-point types that can show rounding errors such as 0.1 + 0.2.
Arithmetic on numeric is exact but slower. ROUND(x, n) rounds to n decimals.
Note:
Use numeric for currency and double precision for scientific data.
Example: NUMERIC/DECIMAL
shop=# SELECT 0.1::double precision + 0.2::double precision AS float_sum, 0.1::numeric + 0.2::numeric AS numeric_sum;
float_sum | numeric_sum
-------------------+-------------
0.30000000000000004 | 0.3
shop=# SELECT ROUND(2.675, 2) AS rounded, 19.99::numeric(10,2) * 3 AS price_times_3;
rounded | price_times_3
---------+---------------
2.68 | 59.97
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Using float for money
- Forgetting precision and scale
- Assuming numeric is as fast as integer
Chapter Summary
- numeric(p,s) is exact
- real and double precision are approximate
- Use numeric for money
- ROUND controls decimals
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: