DECIMAL, FLOAT and DOUBLE
Exact numbers (DECIMAL up to 65 digits) and approximate numbers (FLOAT, DOUBLE): syntax, rounding, limits and how arithmetic behaves.
Syntax#
DECIMAL [(precision [, scale])] [UNSIGNED] -- also NUMERIC, DEC, FIXED
FLOAT [(m, d)] [UNSIGNED] -- also FLOAT4
DOUBLE [(m, d)] [UNSIGNED] -- also DOUBLE PRECISION, REAL, FLOAT8Arguments#
| Name | Meaning | Default | Limit |
|---|---|---|---|
precision | the number of digits | 10 | 1 to 65 |
scale | the digits after the decimal point | 0 | 0 to 30, and not more than precision |
m, d of FLOAT and DOUBLE | display size, read and ignored |
Remarks#
DECIMAL(p,s)is exact. It is kept as text with its scale, so1234.5678in aDECIMAL(8,3)column is stored1234.568(rounded half away from zero) and0.1stays0.1. A value with more digits before the point thanp - sis out of range (error 1264 in strict mode; the nearest value that fits otherwise).DECIMAL(65,30)is the widest.- Arithmetic.
+ - * / % DIVon decimals and whole numbers is exact up to 128 bits. A division gives the scale of the dividend plusdiv_precision_increment(4 by default), rounded half away from zero.SUMof decimals has the argument's scale;AVGfour more places. Mixing a decimal with aFLOATorDOUBLEgives aDOUBLE. - Approximate comparisons. Comparison, ordering and
SUMorAVGof decimal values of more than 15 significant digits go through a double and are approximate; exactness of storage and of+ - * /is not affected. FLOATis single precision (4 bytes) andDOUBLEis double precision (8 bytes). AFLOATis stored rounded to single precision and shown with the digits MySQL shows (3.14stays3.14).- Not a number.
NaNand infinity are refused (error 1264). A negative value in anUNSIGNEDcolumn is out of range. - Rounding.
ROUNDrounds a decimal half away from zero and aFLOATorDOUBLEhalf to even. See Numeric functions. - Display. A decimal is shown with its scale (
ROUND(1.298, 1)is1.3; aDECIMAL(5,2)holding 3 is3.00). A double is shown in its shortest form. - Dialect.
STDDEV,VARIANCEand related aggregates return a double in the MySQL dialect and, for whole-number or decimal arguments, a decimal with four more places in the MariaDB dialect (see Aggregate functions).DOUBLE PRECISION,FLOAT4,FLOAT8are read in both.
Examples#
CREATE TABLE t (f FLOAT, d DOUBLE, p DECIMAL(8,3));
INSERT INTO t (f, d, p) VALUES (0.1, 0.1, 0.005), (1e3, 1.5e-3, -0.004), (-2, -2, -2.5);
INSERT INTO t (p) VALUES (99999); -- strict: error 1264, DECIMAL(8,3) holds up to 99999.999
SELECT COUNT(*), SUM(p), AVG(p), MIN(p), MAX(d) FROM t;
CREATE TABLE a (f4 FLOAT4, f8 FLOAT8, dp DOUBLE PRECISION); -- MariaDB