In a project I am working on, I somewhat found myself doing the same computation at back end and front end. The computation is rounding off the product of a ratio of two numbers and another number i.e. the expression to round of is of the form
x / y * n
In front end I am using JavaScript and back end I am using MariaDB. With x=41, y=60 and n=30, the front end gives me 21 which is correct, because
41 / 60 * 30
is 20.5 but MariaDB's implementation of ROUND()
function gives me 20 when I pass to it this expression!
ROUND(41/60*30,0)
even though it (MariaDB) evaluates
41 / 60 * 30
to the correct result 20.5000
If I pass 20.5 (20.5000) to MariaDB's ROUND()
function, the function gives back the correct result 21! I have summarised this mystery (to me at least) with the following SQL statement
SELECT 41/60*30, ROUND(41/60*30,0), ROUND(20.5,0), ROUND(20.5000,0)
whose output is below...
Is something wrong with the implementation of ROUND()
function in MariaDB?