mysql decimal类型的精度问题

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

如果是小数精度是16位,计算的时候要特别小心,比如balance和locked的类型是decimal(32,16),下面的sql就会有精度问题:


UPDATE accounts SET balance = balance - '2.2345678977654321', locked = locked + '2.2345678977654321' WHERE accounts.id = 1

改成下面的就可以了


UPDATE accounts SET balance = balance - cast('2.2345678977654321' as decimal(32,16)), locked = locked + cast('2.2345678977654321' as decimal(32,16))  WHERE accounts.id = 1

更新:

问题出在用了字符串上,改成如下也就正确了:

UPDATE accounts SET balance = balance - 2.2345678977654321, locked = locked + 2.2345678977654321 WHERE accounts.id = 1

转载于:https://my.oschina.net/u/855913/blog/1936445

你可能感兴趣的:(mysql decimal类型的精度问题)