Problems with entering data in the float field in MYSQL

Asked

Viewed 3,228 times

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?

  • Take the quotes out. Have you tried ?

  • Yes, it was one of the first attempts and it’s still the same

  • 1

    You’re storing this in a float. Use decimal. Check out this answer in the post http://answall.com/questions/106870/roundround_em-resultado-multiplica%C3%A7%C3%A3o to understand why.

  • Right then is I change the field type to decimal?

  • This value is for storing monetary values?

  • See this also: http://stackoverflow.com/questions/2808478/why-does-the-value-of-this-float-change-from-what-it-was-set-to

  • 1

    @rray will be yes. Switching to DECIMAL saw that solved.

Show 2 more comments

1 answer

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

You are not signed in. Login or sign up in order to post.