mysql使用double的坑

  碰到一个很奇葩的问题,mysql默认是四舍五入的,但是,使用double,有时候不四舍五入。

犯罪现场还原

表结构

CREATE TABLE `test` (
  `salary_decimal` decimal(11,2) DEFAULT NULL,
  `salary_double` double(11,2) DEFAULT NULL
) ENGINE=InnoDB ;

表数据

  salary_decimal 和 salary_double 分别插入 -96.845

  很神奇的是,保存后,decimal的是 -96.85 ,double的是-96.84

  然而,把double的精度从2增加到3,mysql会自动从 -96.84 变为 -96.845 差点亮瞎了我的眼睛

引发的问题

  这样引发了两个问题:

  1. 两边数据不一致,一个decimal是-96.85 一个double是-96.84
  2. sum函数,结果不准确。使用double的表,求和的时候,会出问题。sum(-96.84+ -3.15)的结果不是 -99.99 而是 -100.00

结论

  1. 针对问题1,特意找了下资料,发现double本来就是不精准的

    附上官网的解释

    For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keywordFLOAT in parentheses. MySQL also supports this optional precision specification, but the precision value is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. A precision from 24 to 53 results in an 8-byte double-precision DOUBLEcolumn.

  2. 针对问题2,有了问题1的前提,sum的结果自然也是不精准

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值