i'm developing a PHP application and i have one problem when i use HTML text inputs, and this is the first time where i deal with this case. When i try to insert a float value as 34.4 in HTML text input to put it in MySQL DB, but when i go to my DB i find the value 35. I don't know why this value is rounded, i do not use any function like ceil. If someone can help me i'll be so thankful.
-
2What format is the column? – Mike Rockétt Feb 13 '15 at 16:15
-
in my DB it's Decimal – Abdelilah Aassou Feb 13 '15 at 16:26
-
thanks bro you helped me to find the solution, the format of my column was DECIMAL(10,0), and it should be DECIMAL(10,2). – Abdelilah Aassou Feb 13 '15 at 16:30
3 Answers
If you are working with decimal, the default is DECIMAL(10, 0), you should specify the precision and scale, for example:
DECIMAL(10, 4)
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
Reference: http://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
Also, check this answer: How do I interpret precision and scale of a number in a database?
The problem was in DB column type, it was DECIMAL(10,0) for this reason the MySQL rounded the float number to an INT number.

- 148
- 3
- 15