# PostgreSQL 教程: double precision 数据类型

## PostgreSQL double precision 类型简介

``````column_name double precision
``````

``````colum_name float
``````

`double precision`类型的列可以存储范围在`1E-307``1E+308`之间的值，精度至少是 15 位数字。

## PostgreSQL double precision 类型示例

### 1) 基本的 double precision 数据类型示例

``````CREATE TABLE temperatures (
id SERIAL PRIMARY KEY,
location TEXT NOT NULL,
temperature DOUBLE PRECISION
);
``````

``````INSERT INTO
temperatures (location, temperature)
VALUES
('Lab Room 1', 23.5),
('Server Room 1', 21.8),
('Server Room 2', 24.3)
RETURNING *;
``````

`````` id |   location    | temperature
----+---------------+-------------
1 | Lab Room 1    |        23.5
2 | Server Room 1 |        21.8
3 | Server Room 2 |        24.3
(3 rows)
``````

``````SELECT AVG(temperature)
FROM temperatures;
``````

`````` avg
------
23.2
``````

### 2) 存储不精确的数值

``````CREATE TABLE t(c double precision);
``````

``````INSERT INTO t(c) VALUES(0.1), (0.1), (0.1)
RETURNING *;
``````

``````  c
-----
0.1
0.1
0.1
(3 rows)
``````

``````SELECT SUM(c) FROM t;
``````

``````         sum
---------------------
0.30000000000000004
(1 row)
``````

### 3) 插入过小的数字

``````INSERT INTO t(c)
VALUES (1E-400);
``````

``````ERROR:  "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" is out of range for type double precision
``````

## 总结

• `double precision`数据类型表示不精确的数字。
• `double precision``float8``float`是同义词。
• 使用`double precision`类型存储不精确的数字，使用`numeric`类型存储精确的数字。

## 了解更多

PostgreSQL 教程：数据类型

PostgreSQL 文档：浮点类型