A
A
AusTiN2012-01-17 14:02:30
MySQL
AusTiN, 2012-01-17 14:02:30

Unusual MySQL behavior on UPDATE?

Good day.
There is a table with a FLOAT type column (it stores the state of a certain virtual account).
Write-off is made by the following request:

UPDATE client_data SET `client_balance` = (`client_balance` - %сумма%) WHERE `id` = %id клиента%

But it works as follows:
1) It was 1.5, 1 was written off - 0.5 remained (as it should be)
2) It was 1.1, 1 was written off - 0.1 remained (as it should be)
3) It was 1.1, 1.1 was written off - left ( attention) 2.38419E-8 !
Actually, such behavior is not clear. Why is that?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
pel, 2012-01-17
@AusTiN

Read here: dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
Floats store approximate values. Use decimal

S
SadGnome, 2012-01-17
@SadGnome

I advise you to read this: dev.mysql.com/doc/refman/5.1/en/problems-with-float.html
This behavior is due to the peculiarity of storing real numbers. And this applies to almost all programming languages.
It is really better to use the DECIMAL type to store the balance.

M
Melkij, 2012-01-17
@melkij

Because it's a float.
Round or use decimal.

A
amarox, 2012-01-17
@amarox

faced the same problem. changing float type to decimal helps

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question