How to store decimal in MySQL?
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.
The first parameter 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.