Skip to content
WebsiteHosting.com

comparison Data & storage

SQL DECIMAL versus floating-point types

PostgreSQL NUMERIC rounds to the declared decimal scale, then checks whether the result fits the precision. Binary floating point represents a different set of values: a display of 0.1 need not mean an exact tenth.

Latest recorded source check · P0243

What it means

The decimal 0.1 is exactly one tenth. The binary64 floating-point value displayed as 0.1 is slightly larger. An exact decimal type has a limit of its own: whether it can keep the value you enter depends on the column declaration.

In PostgreSQL 18, DECIMAL and NUMERIC are equivalent. For a declaration such as NUMERIC(5,2), the precision (5) is the total digit budget, and the scale (2) sets the number of fractional places. PostgreSQL rounds to the scale before checking whether the result fits in the three remaining places to the left of the decimal point. PostgreSQL numeric types

Try it

Use synthetic values only. This simulation does not query a database.

SQL DECIMAL versus floating-point types

Parse synthetic decimal inputs as strings; compare exact decimal scale/range with an explicitly bounded floating representation and show overflow/rounding cases.

Simulation

Synthetic inputs only. This is not a measurement of your hardware or service.

Computed under the stated assumptions

The decimal coefficient was parsed exactly, then rounded by the declared policy. Binary64 is shown as a separate rational contrast.

SQL DECIMAL versus floating-point types: synthetic scenario inputs

Scenario: Starting example

Each preset replaces all fields. Editing any field preserves the other values exactly, including invalid entries.

Exact decimal → rounding → precision check

Original decimal as exact fraction
-247/200
Rounded numeric value
-1.24
Exceeds precision after rounding
No
Numeric storage verdict
representable-after-rounding
Exactly halfway
Yes
Magnitude rounded upward
Yes

Binary64 is a separate contrast of the original input

Shortest binary64 display
-1.235
Exact binary64 fraction
-5561945539802563/4503599627370496
Exactly equals original decimal
No

Evaluated evidence

Exact raw decimal string
-1.235
Total numeric precision (digits)
5
Fractional scale (digits; no larger than precision in this lab)
2
Rounding policy
postgresql-18-numeric

PostgreSQL 18 numeric subset with nonnegative scale <= precision; finite decimal inputs only. No database query or arithmetic workload. Binary64 contrasts original input, not the already-rounded numeric value; signed-zero distinctions are omitted.

About this model

Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.

Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.

No external request, device command, stored-input history or measurement is performed.

Start with Exact decimal input -1.235, Total numeric precision 5 digits, Fractional scale 2 digits, and Rounding policy postgresql-18-numeric.

  1. Read the input exactly. The simulation validates the decimal text and separates the minus sign, the integer coefficient 1235 (a BigInt), and the input scale 3. No binary floating-point conversion feeds the decimal calculation.
  2. Round to hundredths. Dividing 1235 by 10 gives 123 with a remainder of 5: the input is exactly halfway between -1.23 and -1.24. PostgreSQL numeric breaks ties away from zero. PostgreSQL rounding functions The magnitude therefore increases to 124 hundredths, giving -1.24, not -1.23.
  3. Check the rounded value. -1.24 needs only one of the three available places to the left of the point. It fits NUMERIC(5,2) after rounding.

Now choose Round into precision overflow. The input 99.995 looks as though it fits the two whole-number places of NUMERIC(4,2). But rounding the halfway value to hundredths carries 99.99 up to 100.00. That rounded candidate needs three whole-number places. 100.00 explains the overflow; it is not a stored result.

Reset restores the default inputs and scenario, returns the step and virtual time to zero, and clears errors, history, and comparison. With those defaults restored, change just Exact decimal input to 0.1. The decimal result is 0.10, exactly 1/10. The separate binary64 comparison uses the original input, not the rounded decimal result. Its shortest display is 0.1, but its exact fraction is 3602879701896397/36028797018963968, not 1/10. Try 0.5 instead: the decimal result 0.50 and the binary64 fraction 1/2 are exactly equal.

In practice

Keep the raw decimal token until you have validated and parsed it. For NUMERIC(20,18), 2.000000000000000001 fits as written, but its binary64 value is exactly 2. Converting the token to Number first would erase the final 1 before a decimal check could see it. Validate the declaration controls as text, too: 5.000000000000000001 is not a whole-number precision setting.

The simulation accepts finite decimal text with up to 40 digits, precision from 1 through 38, and scale from zero through precision. Spaces, exponent notation such as 1e3, NaN, infinity, and negative scales are outside this subset. An invalid input yields no result; that is a limit of the lab, not a verdict on every PostgreSQL declaration. By contrast, Inspect the evidence boundary supplies valid decimal text but selects an unknown rounding policy. Without the rule, the simulation gives neither a rounded value nor an overflow verdict.

If an amount must be kept to hundredths, allow two fractional places and enough whole-number places for the rounded amount. If extra fractional digits must be rejected rather than rounded, check the input scale before storage. For binary floating point, judge exactness by the value represented, not by how few digits it takes to print it.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Can rounding cause overflow?

Yes. Numeric 99.995 rounded to two decimal places becomes 100.00; that result exceeds numeric(4,2). The scale rounding happens before the precision check.

Are decimal and numeric equivalent here?

Yes, PostgreSQL treats decimal and numeric as equivalent types. This does not make their constrained scale or precision unlimited.

Why can displayed 0.1 be inexact internally?

A short decimal display need not expose the full stored binary rational. This example shows binary64 0.1 separately from the exact decimal input 1/10.

Does this cover every SQL numeric declaration?

No. The lab covers finite decimal strings and nonnegative scale no greater than precision. PostgreSQL supports additional declarations and special values outside this subset.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL 18: Numeric Types

    8.1.2 Arbitrary Precision Numbers; 8.1.3 Floating-Point Types. Numeric scale rounding precedes the precision overflow check. Decimal and numeric are equivalent here; floating types are inexact.

    Checked .

  2. PostgreSQL 18: Mathematical Functions and Operators

    9.3, round(numeric) and round(numeric,integer). Numeric rounding breaks ties away from zero; floating round behavior may depend on the platform.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.