Home > Database > Mysql Tutorial > body text

mysql 中浮点型与定点型记录_MySQL

WBOY
Release: 2016-05-30 17:11:05
Original
1111 people have browsed it

为了能够引起大家的重视,在介绍浮点数与定点数以前先让大家看一个例子:

mysql> CREATE TABLE test (c1 float(10,2),c2 decimal(10,2));

Query OK, 0 rows affected (0.22 sec)

mysql> insert into test values(131072.32,131072.32);

Query OK, 1 row affected (0.02 sec)

mysql> insert into test values (999998.02, 999998.02);
Query OK, 1 row affected (0.02 sec)

mysql> select * from test;

+-----------+-----------+

| c1  | c2  |

+-----------+-----------+

| 131072.31 | 131072.32 |

| 999998.00 | 999998.02 |

+-----------+-----------+

1 row in set (0.00 sec)
  
Copy after login

 

 

从上面的例子中我们看到c1列的值由131072.32变成了131072.31;999998.02变成了999998.00,这就是浮点数的不精确性造成的。

 

在mysql中float、double(或real)是浮点数,decimal(或numberic)是定点数。

 

浮点数相对于定点数的优点是在长度一定的情况下,浮点数能够表示更大的数据范围;它的缺点是会引起精度问题。

 

在今后关于浮点数和定点数的应用中,大家要记住以下几点:

 

1、浮点数存在误差问题;

 

2、对货币等对精度敏感的数据,应该用定点数表示或存储;

 

3、编程中,如果用到浮点数,要特别注意误差问题,并尽量避免做浮点数比较;

 

4、要注意浮点数中一些特殊值的处理。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!