SQL reference (MySQL and MariaDB dialects)

DECIMAL, FLOAT and DOUBLE

Exact numbers (DECIMAL up to 65 digits) and approximate numbers (FLOAT, DOUBLE): syntax, rounding, limits and how arithmetic behaves.

Syntax#

SQL
DECIMAL [(precision [, scale])] [UNSIGNED]     -- also NUMERIC, DEC, FIXED
FLOAT [(m, d)] [UNSIGNED]                      -- also FLOAT4
DOUBLE [(m, d)] [UNSIGNED]                     -- also DOUBLE PRECISION, REAL, FLOAT8

Arguments#

NameMeaningDefaultLimit
precisionthe number of digits101 to 65
scalethe digits after the decimal point00 to 30, and not more than precision
m, d of FLOAT and DOUBLEdisplay size, read and ignored

Remarks#

  • DECIMAL(p,s) is exact. It is kept as text with its scale, so 1234.5678 in a DECIMAL(8,3) column is stored 1234.568 (rounded half away from zero) and 0.1 stays 0.1. A value with more digits before the point than p - s is out of range (error 1264 in strict mode; the nearest value that fits otherwise). DECIMAL(65,30) is the widest.
  • Arithmetic. + - * / % DIV on decimals and whole numbers is exact up to 128 bits. A division gives the scale of the dividend plus div_precision_increment (4 by default), rounded half away from zero. SUM of decimals has the argument's scale; AVG four more places. Mixing a decimal with a FLOAT or DOUBLE gives a DOUBLE.
  • Approximate comparisons. Comparison, ordering and SUM or AVG of decimal values of more than 15 significant digits go through a double and are approximate; exactness of storage and of + - * / is not affected.
  • FLOAT is single precision (4 bytes) and DOUBLE is double precision (8 bytes). A FLOAT is stored rounded to single precision and shown with the digits MySQL shows (3.14 stays 3.14).
  • Not a number. NaN and infinity are refused (error 1264). A negative value in an UNSIGNED column is out of range.
  • Rounding. ROUND rounds a decimal half away from zero and a FLOAT or DOUBLE half to even. See Numeric functions.
  • Display. A decimal is shown with its scale (ROUND(1.298, 1) is 1.3; a DECIMAL(5,2) holding 3 is 3.00). A double is shown in its shortest form.
  • Dialect. STDDEV, VARIANCE and 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, FLOAT8 are read in both.

Examples#

SQL
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

See also#