Sub topic 4
Converting Between Numeric Types
This chapter assumes you know basic SELECT expressions and common PostgreSQL types. You should also know that a cast changes the type of a value, not necessarily the value’s meaning.
PostgreSQL numeric types
PostgreSQL provides exact integer and decimal types, plus approximate floating-point types.
smallintstores 16-bit integers.integerorintstores 32-bit integers.bigintstores 64-bit integers.numericordecimalstores exact decimal values.realanddouble precisionstore approximate floating-point values.
The integer types have different ranges, but PostgreSQL can usually convert a smaller integer to a larger one without losing information.
Explicit numeric casts
Use CAST or PostgreSQL’s :: operator when you want to control the conversion.
SELECT CAST(42 AS numeric) AS exact_decimal,
42::bigint AS large_integer,
CAST(3.75 AS integer) AS rounded_integer,
(-3.75)::integer AS rounded_negative;Casting numeric to an integer rounds to the nearest whole number. Ties are rounded away from zero. Therefore, 3.75 becomes 4, and -3.75 becomes -4.
A cast is different from integer division:
SELECT 5 / 2 AS integer_division,
5::numeric / 2 AS decimal_division,
CAST(5 AS numeric) / 2 AS another_decimal_division;The first expression returns 2 because both operands are integers. The other expressions return 2.5 because at least one operand is numeric.
Precision, range, and overflow
Converting to a wider integer increases the available range. Converting to a narrower integer can fail if the value is outside that type’s range.
SELECT 32000::smallint AS fits;
SELECT 40000::smallint AS too_large;The second query raises an error because 40000 cannot fit in a smallint.
numeric can represent decimal values exactly within its declared precision and scale. A declaration such as numeric(8,2) allows eight total digits, with two digits after the decimal point.
CREATE TEMP TABLE prices (
amount numeric(8,2)
);
INSERT INTO prices VALUES (1234.56);
SELECT amount,
amount::integer AS rounded_amount
FROM prices;The value remains 1234.56 in the table. The cast only affects the expression in the SELECT.
Approximate types can introduce small representation errors:
SELECT 0.1::double precision + 0.2::double precision AS approximate_result,
0.1::numeric + 0.2::numeric AS exact_result;Use numeric for money, measurements requiring exact decimal arithmetic, and values that must compare precisely. Use floating-point types when a small approximation is acceptable and a wider or faster calculation matters.
Choosing a conversion
| Situation | Suitable choice | Main concern |
|---|---|---|
| Whole-number identifiers | `integer` or `bigint` | Range and overflow |
| Exact decimal amounts | `numeric` | Precision and scale |
| Scientific or approximate calculations | `double precision` | Rounding error |
| Fractional result from integer values | Cast before division | Casting afterward is too late |
Exercise
What does 5 / 2::numeric return, and why?
Show answer
It returns 2.5. The cast applies to 2 before division, so PostgreSQL performs numeric division instead of integer division.