NUMERIC

Numeric data types store fixed point numeric data. For example, a money value of $123.45 can be stored in a NUMERIC(5,2) field.

Syntax

NUMERIC | DECIMAL | NUMBER | MONEY [ ( precision [ , scale ] ) ]

Parameters

precision

The total number of significant digits that the data type stores. precision must be positive and <= 1024. If you assign a value that exceeds the precision value, an error occurs.

scale

The maximum number of digits to the right of the decimal point that the data type stores. scale must be non-negative and less than or equal to precision. If you omit the scale parameter, the scale value is set to 0. If you assign a value with more decimal digits than scale, the value is rounded to scale digits.

Notes

Examples

The following series of commands creates a table that contains a numeric data type and then performs some mathematical operations on the data:

=> CREATE TABLE num1 (id INTEGER, amount NUMERIC(8,2));

Insert some values into the table:

=> INSERT INTO num1 VALUES (1, 123456.78);

Query the table:

=> SELECT * FROM num1;  
  id  |  amount
------+-----------
    1 | 123456.78
(1 row)

The following example returns the NUMERIC column, amount, from table num1:

=> SELECT amount FROM num1;  
  amount
-----------
 123456.78
(1 row)

The following syntax adds one (1) to the amount:

=> SELECT amount+1 AS 'amount' FROM num1;  
  amount
-----------
 123457.78
(1 row)

The following syntax multiplies the amount column by 2:

=> SELECT amount*2 AS 'amount' FROM num1;  
  amount
-----------
 246913.56
(1 row)

The following syntax returns a negative number for the amount column:

=> SELECT -amount FROM num1;  
 ?column?
------------
 -123456.78
(1 row)

The following syntax returns the absolute value of the amount argument:

=> SELECT ABS(amount) FROM num1;    
   ABS
-----------
 123456.78
(1 row)

The following syntax casts the NUMERIC amount as a FLOAT data type:

=> SELECT amount::float FROM num1;  
  amount
-----------
 123456.78
(1 row)