DECIMAL

Exact fixed-point number. DECIMAL(M,D) stores values with M total digits and D decimal places, preventing rounding errors common with floating-point types.

Syntax

DECIMAL[(M[,D])] [SIGNED | UNSIGNED | ZEROFILL]

Description

A packed "exact" fixed-point number. M is the total number of digits (the precision), and D is the number of digits after the decimal point (the scale).

  • The decimal point and (for negative numbers) the "-" sign are not counted in M.

  • If D is 0, values have no decimal point or fractional part, and on INSERT, the value will be rounded to the nearest DECIMAL.

  • The maximum number of digits (M) for DECIMAL is 65.

  • The maximum number of supported decimals (D) is 30 before MariadB 10.2.1 and 38 afterwards.

  • If D is omitted, the default is 0. If M is omitted, the default is 10.

UNSIGNED, if specified, disallows negative values.

ZEROFILL, if specified, pads the number with zeros, up to the total number of digits specified by M.

All basic calculations (+, -, *, /) with DECIMAL columns are done with a precision of 65 digits.

For more details on the attributes, see Numeric Data Type Overview.

DEC, NUMERIC, and FIXED are synonyms, as well as NUMBER in Oracle mode.

Examples

With strict_mode set

SIGNED and UNSIGNED

The DECIMAL data type may be SIGNED (allowing negative values) or UNSIGNED (not allowing negative values).

Example of DECIMAL SIGNED (SIGNED is the default):

Example of DECIMAL UNSIGNED:

Out of Range

A value is considered "out of range" when it is too small or too large to be stored in a data type. The size specified when creating the column is the strict limit for what values can be represented. When SQL_MODE is strict (the default), an out-of-range value generates an error, and the operation fails. If strict mode is not in effect, the value is rounded to the nearest valid value, and a warning is generated (which might be hidden, depending on your warning settings).

A value whose significant digits must be rounded to fit only generates a warning note about data truncation, since it is only an out-of-range value if the rounding causes the value to overflow.

DECIMAL ZEROFILL

A special type of DECIMAL UNSIGNED is DECIMAL ZEROFILL, which pads out the values with leading zeros in SELECT results. The number of leading zeros is just enough to pad the field out to the length of the type's field size (not counting the decimal place), but the zeros are not included in an expression result or in a UNION SELECT column.

Using DECIMAL ZEROFILL works the same way as DECIMAL UNSIGNED for most operations except a simple SELECT. For example, with the following test table setup:

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sql

Last updated

Was this helpful?