I\'ve tried using DECIMAL with (2,2) but it won\'t let me use this.
I simply want to store a number, for example 7.50 or 10.50. I need to keep both numbers after th
The first digit of the DECIMAL
declaration is the total digits. You probably want to use DECIMAL (4, 2)
. This allows for up to two digits before the decimal and two after.
Documentation: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
The syntax is DECIMAL(M,D)
M - total length
D - digits right of the decimal point
http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.6 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.
CREATE TABLE `salary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`salary` DECIMAL(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DECIMAL(10,2)
indicates that salary
will hold a total of 10 digits out of which 2 will be after the decimal.
i.e.
8 digits before decimal and 2 digits after decimal.
CREATE TABLE IF NOT EXISTS `table_name` (`id` int(11) NOT NULL AUTO_INCREMENT,`cost` DECIMAL( 10, 2 ) NOT NULL);
This will make the cost column hold a total of 10 digits, 8 before and 2 after the decimal point.
From mysql doc:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:
- M is the maximum number of digits (the precision). It has a range of 1 to 65.
- 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.
NB:- M is total no. of digits before decimal point + total no. of digits after decimal point.
In your case, 7.50
has a total no of 3 digits and 10.50
has a total no of 4 digits. But the declared maximum no. of digits for the column is 2 so it can store a maximum of two digits value. You cannot even store 1 because it's 1.00 i.e. total 3 digits. So the maximum allowed 2 digit value in the column is .99
.
If you want to store xx.xx then you have to declare (4, 2) where 4 is M and 2 is D.
If you want to store any number at maximum allowed size of mysql then you can declare a column with (65, 30).
Maximum no. of digits before decimal point = M - D
Change your field from INT to FLOAT