简体   繁体   English

MySQL 将 round() 中的小数限制为少于预期

[英]MySQL limits decimals in round() to fewer than expected

I have two different MySQL servers, when i run the following query on both, i get different results:我有两个不同的 MySQL 服务器,当我在两个服务器上运行以下查询时,我得到不同的结果:

SELECT round(1/3,6) -- 0,333333 on Server1
SELECT round(1/3,6) -- 0,3333   on Server2

The first one gives 6 decimals as expected, the second one gives only 4 decimals.第一个给出了预期的 6 位小数,第二个只给出了 4 位小数。

SELECT format(round(1/3,6),6) -- 0,333333 on Server1
SELECT format(round(1/3,6),6) -- 0,333300 on Server2

SELECT round(cast(1 as float)/3,6) -- 0,333333 (6 decimals) on both

SELECT 1/3 -- 0,3333 (4 decimals) on both as expected

The MySQL Version is 8.0.18-9 on Server1 and 8.0.26-17 on Server2. MySQL 版本在 Server1 上为 8.0.18-9,在 Server2 上为 8.0.26-17。 div_precision_increment = 4 on both servers.两台服务器上的 div_precision_increment = 4。

UPDATE:更新:

Its a mysql version issue - when updated to 8.0.26 both servers have the same issue.这是一个 mysql 版本问题 - 当更新到 8.0.26 时,两台服务器都有同样的问题。 But i think its the behaviour of 8.0.26 thats buggy.但我认为 8.0.26 的行为存在问题。

In 8.0.27 there is a bugfix description that might address this issue:在 8.0.27 中有一个可能解决此问题的错误修复说明:

Type resolution for negation did not set the proper precision when converting the type from integer to decimal.将类型从 integer 转换为十进制时,否定的类型解析没有设置正确的精度。 This is fixed by assigning the same precision as the argument.这是通过分配与参数相同的精度来解决的。 (Bug #32863037) (缺陷号 32863037)

But Percona does not offer that update for now, so ill try to downgrade and then wait for 8.0.27+但是 Percona 目前不提供该更新,所以我尝试降级然后等待 8.0.27+

UPDATE:更新:

This is a error in the version, update the version of the 8.0.18 server to the same version这个是版本错误,更新8.0.18服务器的版本到同一个版本

it is a server variable它是一个服务器变量

SET div_precision_increment = 6;

will change for the session the number of digits.将更改为 session 的位数。 This variable indicates the number of digits by which to increase the scale of the result of division operations performed with the / operator.此变量指示要将使用 / 运算符执行的除法运算结果的小数位数增加的位数。 The default value is 4. The minimum and maximum values are 0 and 30, respectively.默认值为 4。最小值和最大值分别为 0 和 30。 The following example illustrates the effect of increasing the default value.以下示例说明了增加默认值的效果。

more information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM