3
I have a problem when inserting a value in a field in the table, this field is like float(15,6).
I ran an Insert here
INSERT INTO valores (valor) VALUES ('1160.480000');
Why it inserts the value as 1160.479980?
3
I have a problem when inserting a value in a field in the table, this field is like float(15,6).
I ran an Insert here
INSERT INTO valores (valor) VALUES ('1160.480000');
Why it inserts the value as 1160.479980?
8
Formalizing the answer:
The mistake happens because the float
does not keep exact details - and approximate, for this (monetary values), we should use the decimal
...
The decimal in Mysql has the ability to store a maximum of 65 digits, and of these, 30 digits can be used for the decimal place.
This reply better detail the whys.
Another post on the subject.
ALTER TABLE valores MODIFY valor decimal(15,6);
INSERT INTO valores (valor) VALUES (1160.480000);
Browser other questions tagged mysql float
You are not signed in. Login or sign up in order to post.
Take the quotes out. Have you tried ?
– Diego Souza
Yes, it was one of the first attempts and it’s still the same
– Marcelo Diniz
You’re storing this in a
float
. Usedecimal
. Check out this answer in the post http://answall.com/questions/106870/roundround_em-resultado-multiplica%C3%A7%C3%A3o to understand why.– Ismael
Right then is I change the field type to decimal?
– Marcelo Diniz
This value is for storing monetary values?
– rray
See this also: http://stackoverflow.com/questions/2808478/why-does-the-value-of-this-float-change-from-what-it-was-set-to
– Laércio Lopes
@rray will be yes. Switching to DECIMAL saw that solved.
– Marcelo Diniz