乘法和除法产生不同比例的小数,为什么?
Multiplication and division produce decimal with different scale, why?
当我从 MySQL 查询数据时,我注意到了这种行为:
select 1 / 100;
-- 0.0100
select 1 * 0.01;
-- 0.01
我尝试运行它们在不同的机器上,但得到相同的结果:除法产生 4 位小数,乘法产生 2 位小数。
谁能告诉我他们为什么不同?
结果数据类型的规则 are described here:
In division performed with /
, the scale of the result when using two
exact-value operands is the scale of the first operand plus the value
of the div_precision_increment
system variable (which is 4 by
default).
所以我们有:
SELECT 1 / 100 -- 0.0100
SELECT 1.0 / 100 -- 0.01000
SELECT 1.00 / 100 -- 0.010000
SELECT 1 / 100.0 -- 0.0100
当我从 MySQL 查询数据时,我注意到了这种行为:
select 1 / 100;
-- 0.0100
select 1 * 0.01;
-- 0.01
我尝试运行它们在不同的机器上,但得到相同的结果:除法产生 4 位小数,乘法产生 2 位小数。
谁能告诉我他们为什么不同?
结果数据类型的规则 are described here:
In division performed with
/
, the scale of the result when using two exact-value operands is the scale of the first operand plus the value of thediv_precision_increment
system variable (which is 4 by default).
所以我们有:
SELECT 1 / 100 -- 0.0100
SELECT 1.0 / 100 -- 0.01000
SELECT 1.00 / 100 -- 0.010000
SELECT 1 / 100.0 -- 0.0100