DEV Community

Scale
Scale

Posted on

GBase 8s TRUNCATE Function: Precise Numeric Control Without Rounding

In real-world database development, precision is often more important than approximation.

While many developers rely on ROUND, there are scenarios where rounding is unacceptable—especially in financial or analytical systems.

This is where the TRUNCATE function in GBase database becomes essential.


🚀 1. What TRUNCATE Really Does

The TRUNCATE function:

  • Cuts off digits without rounding
  • Preserves deterministic values
  • Provides strict precision control

👉 Unlike ROUND, it never alters values beyond simple truncation.


⚙️ 2. Syntax

TRUNCATE(n [, m])
Enter fullscreen mode Exit fullscreen mode


`

Parameters

  • n: numeric value
  • m: number of digits to retain

📊 3. Core Usage Examples

Keep 2 decimal places

sql id="truncate_example_01"
SELECT TRUNCATE(1234.234, 2) FROM dual;

👉 Result:

plaintext
1234.23


Remove decimal part

sql id="truncate_example_02"
SELECT TRUNCATE(1234.234, 0) FROM dual;

👉 Result:

plaintext
1234


Truncate integer part (left side)

sql id="truncate_example_03"
SELECT TRUNCATE(1234.234, -2) FROM dual;

👉 Result:

plaintext
1200


🔍 4. Edge Case Behavior

NULL input

sql id="truncate_null"
SELECT TRUNCATE(NULL, 2) FROM dual;

👉 Returns NULL


Exceeding decimal precision

sql id="truncate_padding"
SELECT TRUNCATE(12.3, 4) FROM dual;

👉 Result:

plaintext
12.3000


⚠️ 5. TRUNCATE vs ROUND

sql id="truncate_vs_round"
SELECT
ROUND(1234.235, 2),
TRUNCATE(1234.235, 2)
FROM dual;

👉 Result:

  • ROUND → 1234.24
  • TRUNCATE → 1234.23

⚡ 6. When Should You Use TRUNCATE?

Use TRUNCATE when:

  • Financial accuracy is required
  • Rounding introduces risk
  • Data must remain deterministic

🧠 7. Best Practice

  • Use TRUNCATE for calculations
  • Use ROUND for display purposes
  • Avoid mixing both without clear intent

📌 Final Thoughts

The GBase database TRUNCATE function is simple but powerful.

It ensures:

  • Exact numeric control
  • Predictable results
  • No hidden rounding errors

Top comments (0)