博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DOUBLE vs DECIMAL 区别
阅读量:6245 次
发布时间:2019-06-22

本文共 2154 字,大约阅读时间需要 7 分钟。

hot3.png

对于财务计算,最好使用DECIMAL类型

The example from MySQL documentation http://dev.mysql.com/doc/refman/5.1/en/problems-with-float.html (i shrink it, documentation for this section is the same for 5.5)

mysql> create table t1 (i int, d1 double, d2 double);

mysql> insert into t1 values (2, 0.00  , 0.00),

                             (2, -13.20, 0.00),

                             (2, 59.60 , 46.40),

                             (2, 30.40 , 30.40);

mysql> select i, sum(d1) as a, sum(d2) as b from t1 group by i having a <> b;

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

| i    | a                 | b    |

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

|    2 | 76.80000000000001 | 76.8 |

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

1 row in set (0.00 sec)

Basically if you sum a you get 0-13.2+59.6+30.4 = 76.8. If we sum up b we get 0+0+46.4+30.4=76.8. The sum of a and b is the same but MySQL documentation says:

A floating-point value as written in an SQL statement may not be the same as the value represented internally.

If we repeat the same with decimal:

mysql> create table t2 (i int, d1 decimal(60,30), d2 decimal(60,30));

Query OK, 0 rows  affected (0.09 sec)

mysql> insert into t2 values (2, 0.00  , 0.00),

                             (2, -13.20, 0.00),

                             (2, 59.60 , 46.40),

                             (2, 30.40 , 30.40);

Query OK, 4 rows affected (0.07 sec)

Records: 4  Duplicates: 0  Warnings: 0

mysql> select i, sum(d1) as a, sum(d2) as b from t2 group by i having a <> b;

Empty set (0.00 sec)

The result as expected is empty set.

So as long you do not perform any SQL arithemetic operations you can use DOUBLE, but I would still prefer DECIMAL.

Another thing to note about DECIMAL is rounding if fractional part is too large. Example:

mysql> create table t3 (d decimal(5,2));

Query OK, 0 rows affected (0.07 sec)

mysql> insert into t3 (d) values(34.432);

Query OK, 1 row affected, 1 warning (0.10 sec)

mysql> show warnings;

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

| Level | Code | Message                                |

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

| Note  | 1265 | Data truncated for column 'd' at row 1 |

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

1 row in set (0.00 sec)

mysql> select * from t3;

+-------+

| d     |

+-------+

| 34.43 |

+-------+

1 row in set (0.00 sec)

转载于:https://my.oschina.net/forrest420/blog/670283

你可能感兴趣的文章
家庭宽带市场竞争分析
查看>>
台媒:手机应用和免费wifi可瞬间泄露隐私
查看>>
QUnit单元测试文档
查看>>
手机网络电话(VOIP)大比拼
查看>>
华天动力OA系统全国渠道布局 20个城市分公司初露端倪
查看>>
我市智慧城市建设迈入快车道
查看>>
FSF 鼓励用户抛弃英特尔
查看>>
编程语言漫谈
查看>>
《Python数据科学实践指南》——0.4节一个简单的例子
查看>>
《树莓派学习指南(基于Linux)》——本章小结
查看>>
中国自主操作系统COS宣传片:很好很强大
查看>>
《SolidWorks 2017中文版机械设计从入门到精通)》——2.2 草图命令
查看>>
Google 开发新的开源系统 Fuchsia
查看>>
社区不是请客吃饭(二)不出国门也能参与OpenStack Summit
查看>>
FreeDOS 诞生二十周年
查看>>
新的 OpenID 基金会的董事会领导
查看>>
第十天:估算活动持续时间,类比估算,参数估算,自下而上估算,三点估算解析表...
查看>>
为什么我要垂直对齐代码(你也要如此!)
查看>>
《ANSYS Workbench 16.0超级学习手册》——1.4 本章小结
查看>>
微软确认周二更新补丁破坏了 Windows 10 重置功能
查看>>