Dear friends from SO!
I'm having an odd issue with a MariaDB 5.5.60 running on linux.
For some reason, I'm doing simple operations with the difference column, as you can see, is a double.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`email` varchar(256) DEFAULT NULL,
`username` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL,
`won` int(11) NOT NULL DEFAULT '0',
`lose` int(11) NOT NULL DEFAULT '0',
`difference` double NOT NULL DEFAULT '0',
`available_balance` double NOT NULL DEFAULT '0',
`pending_received_balance` double NOT NULL DEFAULT '0',
`spawns` int(11) NOT NULL DEFAULT '0',
`condicion` varchar(256) NOT NULL DEFAULT 'offline',
`address` varchar(256) NOT NULL DEFAULT 'offline',
`eliminado` int(12) NOT NULL DEFAULT '0',
`creacion` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
The problem:
-917.3700000000001
This happens when the system starts adding and subtracting values. As these numbers represents money, we can't tolerate any arbitrary difference.
One of the queries that can lead to this problems is the following:
UPDATE users SET
difference = difference - '$xfer->value_final',
available_balance = '$user->available_balance',
pending_received_balance = '$user->pending_received_balance',
condicion = '$user->condicion'
WHERE id = '$user->id'
As you can see, the value is being updated to substract $xfer->value_final, which, in this case is 101.93.
It's even a bit hard to replicate, as I had to place the same order at least 10 times, before getting the long zero tailed value.
Once the value happens once, then, it will of course remain full of zeros for every next operation, or until fixed.
This values are coming from the BTC blockchain, however I'm 100% sure that there's always 2 decimals on them, as I'm always storing them in another table.
¿Does anyone know why this could be happening? ¿Is this a bug? ¿Or am I missing something?
¿How can I solve this?
Thanks in advance; Chris