Curious Minds

Clear answers to science and history questions

Why Firebird Truncates Decimal Places in Division

· 6 min read · Science

Firebird truncates decimal places when dividing because the SQL standard and Firebird's own type rules determine the result scale from the operands' declared precision and scale, and division is defined to produce a scale that often drops fractional digits rather than rounding them. In practice, dividing two NUMERIC or DECIMAL values in Firebird yields a result whose scale is calculated as s1 - s2 + p1 (where p1 and s1 are the dividend's precision and scale, and s2 is the divisor's scale), and any digits beyond that scale are silently discarded. This behavior surprises many users because it differs from typical programming languages, which usually round or keep full precision.

Background: How Firebird Stores Numeric Types

Firebird supports two exact numeric types: NUMERIC and DECIMAL. Both are stored as fixed-point integers with an explicit precision (total number of significant digits) and scale (number of digits after the decimal point). For example, NUMERIC(8,2) can store values up to 999,999.99 with exactly two decimal places. The key point is that Firebird does not use floating-point arithmetic for these types; it uses integer arithmetic with a fixed decimal point. This design guarantees exactness for addition, subtraction, and multiplication, but division is inherently different because the result may require more decimal places than the operands provide.

How Firebird Determines the Result Scale for Division

Firebird's division rule is defined in its documentation and follows the SQL standard's approximate model. For a / b, where a has precision p1 and scale s1, and b has precision p2 and scale s2, the result scale is:

  • s1 - s2 + p1

The result precision is capped at 18 (or 34 for NUMERIC with higher precision, but the default is 18). If the computed scale exceeds the maximum allowed (which is 18 for the default), Firebird reduces it to fit, effectively truncating the extra digits. For example, dividing NUMERIC(8,2) by NUMERIC(8,2) gives a scale of 2 - 2 + 8 = 8. So 10.00 / 3.00 yields 3.33333333 (eight decimal places), not 3.333333333333.... If the dividend has a smaller precision, the scale is smaller, and you get fewer decimal places.

Why It Truncates Instead of Rounding

Firebird truncates (discards extra digits) rather than rounding because the SQL standard specifies that the result of division is exact to the computed scale, and any additional digits are simply not represented. Firebird does not apply rounding; it just drops the digits beyond the allowed scale. This is a deliberate choice to keep the operation deterministic and to avoid the complexity of rounding rules. In contrast, many programming languages (like Python or JavaScript) use floating-point division, which keeps a much larger number of digits and then rounds when printing. Firebird's approach is more like integer division with a fixed decimal point, but it does not round to the nearest value—it truncates.

How to Control the Result: Casting and Rounding

If you need more decimal places or want rounding, you have several options:

  1. Cast the operands to a higher precision: For example, CAST(a AS NUMERIC(18,6)) / b will give a result scale of 6 - s2 + 18, which is usually large enough to retain many digits. But be careful: the result scale is still capped at 18, so casting to very high precision may still truncate if the computed scale exceeds 18.
  2. Use the ROUND function: Wrap the division in ROUND(a / b, n) to round to n decimal places. This is the most straightforward way to get a rounded result.
  3. Use CAST on the result: Cast the division result to a specific type, e.g., CAST(a / b AS NUMERIC(10,4)). This will round to 4 decimal places because casting to a lower scale rounds, not truncates.

For example, to get a rounded division to 2 decimal places: ROUND(10.00 / 3.00, 2) returns 3.33. To get more digits without rounding, cast the dividend to a larger precision: CAST(10.00 AS NUMERIC(18,2)) / 3.00 gives a scale of 2 - 2 + 18 = 18, so you get 3.333333333333333333 (18 threes).

Common Problems and Misconceptions

One frequent misconception is that Firebird is buggy or loses data. In reality, the behavior is consistent with the SQL standard, but it can lead to unexpected results in financial calculations. For example, if you divide a monetary amount by a quantity and store the result in a NUMERIC(10,2) column, the value is truncated to two decimal places, which may cause rounding errors over many rows. A common workaround is to divide by a decimal with a larger scale, or to use ROUND explicitly. Another issue is that dividing by zero raises an error, but dividing by a very small number can produce a result that exceeds the maximum precision, causing an overflow error. Always check the precision and scale of your operands.

Comparisons with Other Databases

Firebird's behavior is similar to PostgreSQL, which also uses the SQL standard's rule for numeric division. PostgreSQL, however, rounds the result to the computed scale rather than truncating. For example, in PostgreSQL, 10.00 / 3.00 returns 3.33333333 (rounded to 8 decimals), not truncated. MySQL, on the other hand, uses a different rule: it returns a result with a scale based on the operands, but it also rounds. SQL Server uses a similar rule but with a maximum scale of 38. The key difference is that Firebird truncates, while most others round. This is why users coming from other databases may be surprised.

Practical Tips for Avoiding Truncation

  • Always declare numeric columns with a scale that matches your needs. If you need to store fractional results, use a scale of at least 4 or 6.
  • When performing division in a query, consider casting the dividend to a higher precision to get more decimal places.
  • Use ROUND when you need a specific number of decimal places and are okay with rounding.
  • Test your division queries with sample data to see the actual scale produced.

Frequently Asked Questions

Why does Firebird give me 0.00 when I divide two integers?

If both operands are integers (e.g., NUMERIC(10,0)), the result scale is 0 - 0 + 10 = 10, but the result is then cast to the column type if you store it. If you select directly, you get 3.3333333333 (10 decimals). If you store it in a NUMERIC(10,2) column, it truncates to 3.33.

Can I get a rounded result without using ROUND?

Yes, you can cast the result to a lower scale, which rounds. For example, CAST(10.00 / 3.00 AS NUMERIC(10,2)) gives 3.33 (rounded).

Does Firebird ever round division results?

No, division itself never rounds. Only casting to a lower scale or using the ROUND function rounds. Truncation is the default behavior.

Why does my division result have fewer decimal places than I expect?

Because the result scale is computed from the operands' precision and scale. If your dividend has a small precision, the result scale is small. For example, NUMERIC(4,2) / NUMERIC(4,2) gives a scale of 2 - 2 + 4 = 4, so you get 4 decimal places.

Is there a way to force Firebird to use floating-point division?

Yes, you can cast one operand to DOUBLE PRECISION or FLOAT, which uses binary floating-point arithmetic and does not truncate. However, this may introduce rounding errors due to binary representation.

In this article
  1. Background: How Firebird Stores Numeric Types
  2. How Firebird Determines the Result Scale for Division
  3. Why It Truncates Instead of Rounding
  4. How to Control the Result: Casting and Rounding
  5. Common Problems and Misconceptions
  6. Comparisons with Other Databases
  7. Practical Tips for Avoiding Truncation
  8. Frequently Asked Questions