问题描述
- mysql浮点型输出格式问题
-
drop table if exists d;
create table d(c double) engine=innodb;
insert into d values(1e-15),(1e-16);
select * from d;
在mysql数据库按以上语句建表,输出结果如下:
+-------------------+
| c |
+-------------------+
| 0.000000000000001 |
| 1e-16 |
+-------------------+
为什么1e-15按照正常输出,1e-16输出为1e-16,跪求mysql高人详细解释,感激不尽。
解决方案
我估计你用个MySQL版本是5.5,我用的是5.1,处理的方式是小数点后5位开始使用科学计算法,5.5的处理方式是小数点后16位开始使用科学计算法。如果你在字段类型声明中没有指定M和D的值,MySQL就会这样去处理double类型的小数点,如果你指定了M和D值,MySQL就会按照你指定的精度显示。关于M和D的说明如下:
mysql> ? double
Name: 'DOUBLE'
Description:
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
A normal-size (double-precision) floating-point number. Permissible
values are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and
2.2250738585072014E-308 to 1.7976931348623157E+308. These are the
theoretical limits, based on the IEEE standard. The actual range might
be slightly smaller depending on your hardware or operating system.
M is the total number of digits and D is the number of digits following
the decimal point. If M and D are omitted, values are stored to the
limits permitted by the hardware. A double-precision floating-point
number is accurate to approximately 15 decimal places.
UNSIGNED, if specified, disallows negative values.
URL: http://dev.mysql.com/doc/refman/5.1/en/numeric-type-overview.html