How the Scale() Function Works in PostgreSQL

In PostgreSQL, the scale() function returns the scale of its argument.

The scale is the number of decimal digits in the fractional part.

Syntax

The syntax goes like this:

scale(numeric)

Where numeric is the number for which you want the scale returned.

Example

Here’s an example of how it works.

SELECT scale(0.1234);

Result:

4

Here are some more.

SELECT 
  scale(0) AS "0",
  scale(0.1) AS "0.1",
  scale(0.12) AS "0.12",
  scale(0.123456789) AS "0.123456789";

Result:

 0 | 0.1 | 0.12 | 0.123456789
---+-----+------+-------------
 0 |   1 |    2 |           9

Negative Numbers

It works on negative numbers just the same.

SELECT 
  scale(0) AS "0",
  scale(-0.1) AS "-0.1",
  scale(-0.12) AS "-0.12",
  scale(-0.123456789) AS "-0.123456789";

Result:

 0 | -0.1 | -0.12 | -0.123456789
---+------+-------+--------------
 0 |    1 |     2 |            9

Expressions

You can also provide expressions, such as the following.

SELECT 
  1.23 * 0.3 AS "Result of expression",
  scale(1.23 * 0.3);

Result:

 Result of expression | scale
----------------------+-------
                0.369 | 3