# PostgreSQL 教程: numeric 数值类型

## PostgreSQL NUMERIC 数据类型简介

`NUMERIC`类型可以存储具有很多位数的数字。通常，您会将`NUMERIC`类型用于需要精确性的数字，例如货币金额或数量。

``````NUMERIC(precision, scale)
``````

`NUMERIC`类型最多可以保存小数点前`131,072`位和小数点后`16,383`位的值。

`NUMERIC`类型的小数位数可以为零或正数。下面显示了小数位数为零的`NUMERIC`类型的语法：

``````NUMERIC(precision)
``````

``````NUMERIC
``````

## PostgreSQL NUMERIC 示例

### 1) 存储 numeric 数值

``````DROP TABLE IF EXISTS products;

CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(5,2)
);
``````

``````INSERT INTO products (name, price)
VALUES ('Phone',500.215),
('Tablet',500.214);
``````

``````SELECT * FROM products;
``````

``````INSERT INTO products (name, price)
VALUES('Phone',123456.21);
``````

PostgreSQL 发出以下错误：

``````ERROR:  numeric field overflow
DETAIL:  A field with precision 5, scale 2 must round to an absolute value less than 10^3.
``````

### PostgreSQL NUMERIC 类型和 NaN

``````UPDATE products
SET price = 'NaN'
WHERE id = 1;
``````

``````SELECT * FROM products;
``````

``````SELECT * FROM products
ORDER BY price DESC;
``````

## 了解更多

PostgreSQL 教程：数据类型

PostgreSQL 文档：任意精度数字