Explained clearly 6 min read

Banker’s Rounding in SQL: How ROUND() Behaves in PostgreSQL, MySQL, and SQL Server

SQL ROUND() rarely implements banker's rounding consistently. PostgreSQL, MySQL, and SQL Server differ by data type: exact numerics usually round half away from zero, while floating-point may round half to even.

Short Answer

SQL ROUND() rarely implements banker's rounding consistently. PostgreSQL, MySQL, and SQL Server differ by data type: exact numerics usually round half away from zero, while floating-point may round half to even.

Rule Statement

Banker’s rounding, also called round half to even, is a tie-breaking rule: when a value is exactly halfway between two representable results, it is rounded to the nearest even digit. For example, 2.5 becomes 2, 3.5 becomes 4, -2.5 becomes -2, and -3.5 becomes -4. This differs from half-up rounding and from half-away-from-zero rounding, which send 2.5 to 3 and -2.5 to -3.

In SQL, the built-in ROUND() function in PostgreSQL, MySQL, and SQL Server does not uniformly implement banker’s rounding. The exact behavior depends on the database engine, the numeric data type, and sometimes the host platform’s C library. The most important distinction is between exact numeric types such as DECIMAL and NUMERIC, and approximate numeric types such as FLOAT, DOUBLE, and DOUBLE PRECISION.

Software Behavior Note

PostgreSQL

PostgreSQL’s round(numeric) and round(numeric, s) round half away from zero. Thus SELECT round(2.5::numeric); returns 3, and SELECT round(-2.5::numeric); returns -3. This is not banker’s rounding.

For double precision, PostgreSQL typically delegates to the C library’s rint function, which often rounds half to even. Therefore SELECT round(2.5::float8); may return 2 on many systems. However, this behavior is platform-dependent and should not be treated as a portable SQL guarantee.

MySQL

MySQL documents that for exact-value numbers, ROUND() uses the half-away-from-zero rule. For example, SELECT ROUND(2.5); returns 3 because 2.5 is treated as an exact DECIMAL. For approximate-value numbers such as FLOAT and DOUBLE, the result depends on the C library. On many systems, SELECT ROUND(2.5e0); returns 2 because the approximate value is rounded half to even. This is not a reliable banker’s rounding implementation.

SQL Server

SQL Server’s ROUND() always rounds half away from zero. SELECT ROUND(2.5, 0); returns 3, and SELECT ROUND(-2.5, 0); returns -3. T-SQL has no native banker’s rounding in ROUND(). The .NET Math.Round method defaults to banker’s rounding, but that is a .NET behavior, not a T-SQL behavior.

Convention Comparison Table

Database Data type ROUND(2.5) ROUND(-2.5) Tie rule
PostgreSQL numeric 3 -3 Half away from zero
PostgreSQL double precision 2 (often) -2 (often) Half to even, platform-dependent
MySQL DECIMAL 3 -3 Half away from zero
MySQL DOUBLE 2 (often) -2 (often) Half to even, C library dependent
SQL Server decimal/float 3 -3 Half away from zero

The table assumes exact 0.5 ties. In floating-point arithmetic, the stored value may be slightly above or below 0.5, which can change the result under any rounding rule.

Worked Examples

Example 1: PostgreSQL numeric versus float8

  1. Query: SELECT round(2.5::numeric), round(2.5::float8);
  2. The numeric path sees exact 2.5, a tie, and applies half away from zero, producing 3.
  3. The float8 path stores an approximate binary value and often applies half to even, producing 2.
  4. Conclusion: the same literal can yield different results depending on type.

Example 2: MySQL DECIMAL versus DOUBLE

  1. Query: SELECT ROUND(2.5), ROUND(2.5e0);
  2. 2.5 is an exact DECIMAL, so ROUND() returns 3.
  3. 2.5e0 is an approximate DOUBLE, so ROUND() often returns 2 on systems using half-even C library rounding.
  4. Conclusion: do not assume MySQL ROUND() is uniformly banker’s rounding.

Example 3: SQL Server negative tie

  1. Query: SELECT ROUND(-2.5, 0);
  2. SQL Server returns -3.
  3. Banker’s rounding would return -2 because -2 is the nearest even integer.
  4. Conclusion: SQL Server uses half away from zero, not half to even.

Counter-Examples

Counter-example 1: Assuming ROUND(2.5, 0) returns 2 in SQL Server. It returns 3. SQL Server does not use banker’s rounding.

Counter-example 2: Assuming MySQL always uses banker’s rounding because ROUND(2.5e0) returns 2. That result is an artifact of approximate floating-point and the C library, not a guaranteed SQL rule.

Counter-example 3: Assuming PostgreSQL numeric uses banker’s rounding. round(2.5::numeric) returns 3, not 2.

Counter-example 4: Using ROUND() on a computed float and expecting an exact tie. Floating-point error may turn 2.5 into 2.4999999999999996, which rounds to 2 under any rule.

Standards Citation

Banker’s rounding is not merely a programming curiosity; it appears in metrology and data-reporting standards:

  • ASTM E29-13a, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications, Section 6, specifies rounding to the nearest value; when the digit dropped is exactly 5 with no following nonzero digits, round to the nearest even digit.
  • ISO 80000-1:2009, Quantities and units — Part 1: General, Clause 7.3.3, recommends round half to even for numerical values in standards and scientific communication.
  • NIST SP 811, Guide for the Use of the International System of Units (SI), Section 7.9, discusses rounding and significant digits; ties are generally resolved to the nearest even digit.
  • JCGM 100:2008 (GUM), Evaluation of measurement data — Guide to the expression of uncertainty in measurement, Clause 7.2.6, recommends consistent rounding of uncertainty values, often using half-even rules.

SQL engines generally do not follow these documentary standards by default. You must verify each engine’s ROUND() semantics or implement the rule explicitly.

Common Mistakes

  • Assuming that SQL ROUND means banker’s rounding. It usually means half away from zero for exact types.
  • Ignoring data type. DECIMAL/NUMERIC and FLOAT/DOUBLE can behave differently in the same database.
  • Relying on platform-dependent floating-point behavior in production queries.
  • Double rounding: rounding first to fewer decimals, then to final precision, can create errors. See Double Rounding Error.
  • Confusing banker’s rounding with truncation or floor. See Truncation and Ceiling and Floor.
  • Using ROUND() for significant figures when you need half-even. For sig-fig workflows, see Significant Figures and Rounding vs Significant Figures.

Quick Reference Table

Need PostgreSQL MySQL SQL Server
Half away from zero numeric ROUND DECIMAL ROUND ROUND always
Half to even (banker’s) Not in numeric ROUND; float8 may Not guaranteed; DOUBLE may Not in T-SQL ROUND
Implement banker’s manually CASE + even check CASE + even check CASE + even check

For manual banker’s rounding in SQL, compare the fractional part to 0.5 exactly, then round to even when tied. Prefer exact DECIMAL arithmetic and avoid floating-point types for tie-sensitive logic. Related rules include Banker’s Rounding, Round Half Down, and Rounding Methods.

Sources & Further Reading

  • PostgreSQL Documentation, Mathematical Functions and Operators: round.
  • MySQL Reference Manual, Mathematical Functions: ROUND().
  • Microsoft SQL Server Documentation, ROUND (Transact-SQL).
  • ASTM E29-13a, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications.
  • ISO 80000-1:2009, Quantities and units — Part 1: General, Clause 7.3.3.
  • NIST SP 811, Guide for the Use of the International System of Units (SI), Section 7.9.

This site is a precision and rounding reference, not just a calculator. Explore related rules such as Banker’s Rounding, Half-Up Rounding, and Round Half Down.

FAQ

Does SQL Server ROUND() use banker's rounding?

No. SQL Server ROUND() uses half away from zero. ROUND(2.5, 0) returns 3 and ROUND(-2.5, 0) returns -3.

Does PostgreSQL ROUND() use banker's rounding?

For numeric, no: ties round away from zero. For double precision, it may use half-to-even depending on the platform's C library, but this is not a reliable SQL-level guarantee.

Does MySQL ROUND() use banker's rounding?

For exact DECIMAL values, no: ties round half away from zero. For approximate FLOAT/DOUBLE values, behavior depends on the C library and often appears as half-to-even, but it is not guaranteed.

How do I implement banker's rounding in SQL?

Use exact DECIMAL arithmetic and a CASE expression: if the fractional part is exactly 0.5, round toward the nearest even integer; otherwise use normal nearest rounding. Avoid FLOAT/DOUBLE for tie-sensitive logic.

Verified sources

References

  1. PostgreSQL Documentation, Mathematical Functions and Operators: round.
  2. MySQL Reference Manual, Mathematical Functions: ROUND().
  3. Microsoft SQL Server Documentation, ROUND (Transact-SQL).
  4. ASTM E29-13a, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications.
  5. ISO 80000-1:2009, Quantities and units — Part 1: General, Clause 7.3.3.
  6. NIST SP 811, Guide for the Use of the International System of Units (SI), Section 7.9.

Leave a Reply

Your email address will not be published. Required fields are marked *