← Back to PostgreSQL Course | Chapter 3: Data Types | Lesson 5 of 7

NUMERIC/DECIMAL

numeric stores exact decimal numbers, which makes it the right choice for money.

In this page:

  1. NUMERIC/DECIMAL
Syntax
sql
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

bash
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/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using float for money
  2. Forgetting precision and scale
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.