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])
`
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)