Function in mysql does not make the division

I'm working on a function in MySQL Workbench and that function calculates the IVA of a price

The function is:

DELIMITER $$

CREATE FUNCTION PrecoComIva (preco DECIMAL(10,4), iva DECIMAL)
RETURNS DECIMAL(10,4)
DETERMINISTIC
BEGIN
    DECLARE precoIva DECIMAL(10,4);
    SET iva = (iva DIV 100);
    
    SET precoIva = preco + (preco*iva);

    RETURN precoIva;

END $$

DELIMITER ;

But on the line SET iva = (iva DIV 100); the function seems to skip it and does not make the division and returns only the price that the user gave to him

Example: When I run this line SELECT PrecoComIva(20,23); THe result was supposed to be 24,6 but instead is 20

What am I doing wrong?


The div operator is used for integer division. You are probably getting 0 as iva. Use iva / 100 instead.

Also the DECIMAL datatype defaults to scale 0 (no decimal digits). You can use FLOAT for iva for more accuracy.

See https://www.w3resource.com/mysql/mathematical-functions/mysql-div-operator.php

https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html