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)