precision numbers</primary>
</indexterm>
<indexterm>
<primary>decimal</primary>
<see>numeric</see>
</indexterm>
<para>
The type <type>numeric</type> can store numbers with a
very large number of digits. It is especially recommended for
storing monetary amounts and other quantities where exactness is
required. Calculations with <type>numeric</type> values yield exact
results where possible, e.g., addition, subtraction, multiplication.
However, calculations on <type>numeric</type> values are very slow
compared to the integer types, or to the floating-point types
described in the next section.
</para>
<para>
We use the following terms below: The
<firstterm>precision</firstterm> of a <type>numeric</type>
is the total count of significant digits in the whole number,
that is, the number of digits to both sides of the decimal point.
The <firstterm>scale</firstterm> of a <type>numeric</type> is the
count of decimal digits in the fractional part, to the right of the
decimal point. So the number 23.5141 has a precision of 6 and a
scale of 4. Integers can be considered to have a scale of zero.
</para>
<para>
Both the maximum precision and the maximum scale of a
<type>numeric</type> column can be
configured. To declare a column of type <type>numeric</type> use
the syntax:
<programlisting>
NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
</programlisting>
The precision must be positive, while the scale may be positive or
negative (see below). Alternatively:
<programlisting>
NUMERIC(<replaceable>precision</replaceable>)
</programlisting>
selects a scale of 0. Specifying:
<programlisting>
NUMERIC
</programlisting>
without any precision or scale creates an <quote>unconstrained
numeric</quote> column in which numeric values of any length can be
stored, up to the implementation limits. A column of this kind will
not coerce input values to any particular scale, whereas
<type>numeric</type> columns with a declared scale will coerce
input values to that scale. (The <acronym>SQL</acronym> standard
requires a default scale of 0, i.e., coercion to integer
precision. We find this a bit useless. If you're concerned
about portability, always specify the precision and scale
explicitly.)
</para>
<note>
<para>
The maximum precision that can be explicitly specified in
a <type>numeric</type> type declaration is 1000. An
unconstrained <type>numeric</type> column is subject to the limits
described in <xref linkend="datatype-numeric-table"/>.
</para>
</note>
<para>
If the scale of a value to be stored is greater than the declared
scale of the column, the system will round the value to the specified
number of fractional digits. Then, if the number of digits to the
left of the decimal point exceeds the declared precision minus the
declared scale, an error is raised.
For example, a column declared as
<programlisting>
NUMERIC(3, 1)
</programlisting>
will round values to 1 decimal place and can store values between
-99.9 and 99.9, inclusive.
</para>
<para>
Beginning in <productname>PostgreSQL</productname> 15, it is allowed
to declare a <type>numeric</type> column with a negative scale. Then
values will be rounded to the left of the decimal point. The
precision still represents the maximum number of non-rounded digits.
Thus, a column declared as
<programlisting>
NUMERIC(2, -3)
</programlisting>
will round values to the nearest thousand and can store values
between -99000 and 99000, inclusive.
It is also allowed to declare a scale larger than the declared
precision. Such a column can only hold fractional values, and it
requires the number of zero digits just to the