Multiplication and division produce decimal with different scale, why?

Siam :

When i query data from MySQL, I noticed this behavior:

select 1 / 100; 
-- 0.0100
select 1 * 0.01;
-- 0.01

I try to run they in different machine, but get the same result: division produces 4 decimal places, multiplication produces 2 decimal places.

Can someone tell me why they different?

Salman A :

The rules for resulting datatype 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).

So we have:

SELECT 1    / 100   -- 0.0100
SELECT 1.0  / 100   -- 0.01000
SELECT 1.00 / 100   -- 0.010000

SELECT 1   / 100.0  -- 0.0100

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27754&siteId=1