PostgreSQL scale() Function

The PostgreSQL scale() function returns the number of decimal digits in the fractional part for a given number.

scale() Syntax

This is the syntax of the PostgreSQL scale() function:

scale(numeric_value) -> integer

Parameters

numeric_value

Required. The number to process.

Return value

The PostgreSQL scale(numeric_value) function returns the number of decimal digits in the fractional part for a given number.

If the argument is an integer, the scale() function will return 0.

The scale() function will return NULL if the argument is NULL.

PostgreSQL will give an error if you supply a parameter that is not a numeric type.

scale() Examples

Here are a few examples of the scale() function:

This statement uses the scale() function to get the number of decimal digits in fractional part of 1.23000:

SELECT scale(1.23000) AS "scale(1.23000)";
 scale(1.23000)
----------------
              5

Note that this function is different from the min_scale() function, which returns the smallest number of decimal places that can accurately represent a number, for example:

SELECT min_scale(1.23000) AS "min_scale(1.23000)";
 min_scale(1.23000)
--------------------
                  2

If the argument is an integer, the scale() function will return 0.

SELECT
    scale(0) AS "scale(0)",
    scale(1) AS "scale(1)",
    scale(2) AS "scale(2)",
    scale(123) AS "scale(123)";
 scale(0) | scale(1) | scale(2) | scale(123)
----------+----------+----------+------------
        0 |        0 |        0 |          0