Skip to content

Commit dca6a83

Browse files
committed
docs: Improve decimal concept page
1 parent e4f28f9 commit dca6a83

File tree

1 file changed

+12
-67
lines changed

1 file changed

+12
-67
lines changed

documentation/concept/decimal.md

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description:
66
practices for financial and scientific calculations.
77
---
88

9-
QuestDB provides a `decimal` data type for exact numeric calculations, essential
9+
QuestDB provides a `decimal` data type for exact numeric calculations, useful
1010
for financial computations, scientific measurements, and any scenario where
1111
precision matters. This page explains how to use decimals effectively, including
1212
syntax, operations, and performance considerations.
@@ -18,15 +18,6 @@ operations. Unlike floating-point types (`float` and `double`), decimals avoid
1818
rounding errors by storing numbers as scaled integers internally. This makes
1919
them ideal for monetary calculations where accuracy is critical.
2020

21-
### Key characteristics
22-
23-
- **Exact arithmetic**: No rounding errors during addition, subtraction, and
24-
multiplication
25-
- **Fixed precision**: Numbers are stored with a defined number of total digits
26-
- **Fixed scale**: A specific number of digits after the decimal point
27-
- **Variable storage**: Automatically optimized from 1 to 32 bytes based on
28-
precision
29-
3021
## Decimal type in QuestDB
3122

3223
QuestDB implements decimals with the syntax `DECIMAL(precision, scale)`:
@@ -40,9 +31,10 @@ For example, `DECIMAL(10, 2)` can store values from -99,999,999.99 to
4031
If neither the precision and scale are provided, the type defaults to a
4132
precision of 18 and a scale of 3.
4233

43-
### Storage optimization
34+
### Storage
4435

45-
QuestDB automatically selects the optimal storage size based on precision:
36+
QuestDB automatically selects the optimal storage size based on the decimal's
37+
precision:
4638

4739
| Precision | Storage Size | Internal Type |
4840
| ------------ | ------------ | ------------- |
@@ -194,7 +186,8 @@ SELECT CAST(99.99m AS DOUBLE); -- Result: 99.99 (as floating-point)
194186

195187
- **No implicit conversion from double/float**: Must use explicit `CAST` or
196188
decimal literals
197-
- **Integer to decimal**: Safe, no precision loss
189+
- **Integer to decimal**: Safe, no precision loss, the decimals have a scale of
190+
0
198191
- **Double to decimal**: May lose precision due to floating-point representation
199192
- **Between decimal types**: Automatic when precision/scale allows
200193

@@ -210,16 +203,13 @@ SELECT CAST(99.99m AS DOUBLE); -- Result: 99.99 (as floating-point)
210203

211204
- **Slower than floating-point**: Typically slower than `double` operations
212205
- **More storage**: May use more space than `float` for equivalent range
213-
- **Complex operations**: Division and advanced math functions have overhead
206+
- **Complex operations**: Division have overhead
214207

215208
### Performance tips
216209

217-
1. **Use appropriate precision**: Don't over-specify precision beyond your needs
218-
2. **Keep precision ≤ 18 when possible**: DECIMAL64 operations are faster than
219-
DECIMAL128/256
220-
3. **Pre-calculate when possible**: Store commonly used calculations
221-
4. **Consider doubles for analysis**: Use decimals for storage, doubles for
222-
complex analytics
210+
- **Use appropriate precision**: Don't over-specify precision beyond your needs
211+
- **Keep precision ≤ 18 when possible**: DECIMAL64 operations are faster than
212+
DECIMAL128/256
223213

224214
## Common use cases
225215

@@ -298,15 +288,15 @@ SAMPLE BY 1h;
298288

299289
### When to use decimals
300290

301-
**Use decimals for:**
291+
**Use decimals for:**
302292

303293
- Financial data (prices, amounts, exchange rates)
304294
- Accounting calculations
305295
- Scientific measurements requiring exact precision
306296
- Regulatory compliance scenarios
307297
- Any calculation where rounding errors are unacceptable
308298

309-
**Avoid decimals for:**
299+
**Avoid decimals for:**
310300

311301
- Scientific calculations requiring extensive math functions
312302
- Performance-critical analytics on large datasets
@@ -348,48 +338,3 @@ SAMPLE BY 1h;
348338
-- Good: Use decimal literal
349339
SELECT amount + 10.00m FROM prices;
350340
```
351-
352-
## Differences from doubles
353-
354-
| Aspect | Decimal | Double |
355-
| ------------------- | ------------------------- | --------------------- |
356-
| **Precision** | Exact | Approximate |
357-
| **Rounding errors** | None for basic operations | Can accumulate |
358-
| **Performance** | Slower | Faster |
359-
| **Storage** | Variable (1-32 bytes) | Fixed (8 bytes) |
360-
| **Use case** | Money, exact values | Scientific, analytics |
361-
| **Literal syntax** | Requires 'm' suffix | Standard notation |
362-
363-
## Example: Financial reporting
364-
365-
Here's a complete example showing decimals in a financial reporting scenario:
366-
367-
```questdb-sql
368-
-- Create financial tables
369-
CREATE TABLE account_transactions (
370-
account_id SYMBOL,
371-
transaction_type SYMBOL,
372-
amount DECIMAL(15, 2),
373-
balance DECIMAL(15, 2),
374-
timestamp TIMESTAMP
375-
) timestamp(timestamp);
376-
377-
-- Insert sample transactions
378-
INSERT INTO account_transactions VALUES
379-
('ACC001', 'DEPOSIT', 1000.00m, 1000.00m, '2024-01-01'),
380-
('ACC001', 'WITHDRAWAL', 150.50m, 849.50m, '2024-01-02'),
381-
('ACC001', 'INTEREST', 2.12m, 851.62m, '2024-01-03'),
382-
('ACC002', 'DEPOSIT', 5000.00m, 5000.00m, '2024-01-01'),
383-
('ACC002', 'TRANSFER', 500.00m, 4500.00m, '2024-01-02');
384-
385-
-- Financial summary with exact calculations
386-
SELECT
387-
account_id,
388-
sum(CASE WHEN transaction_type = 'DEPOSIT' THEN amount ELSE 0.00m END) AS total_deposits,
389-
sum(CASE WHEN transaction_type = 'WITHDRAWAL' THEN amount ELSE 0.00m END) AS total_withdrawals,
390-
last(balance) AS current_balance,
391-
count(*) AS transaction_count
392-
FROM account_transactions
393-
WHERE timestamp >= '2024-01-01'
394-
GROUP BY account_id;
395-
```

0 commit comments

Comments
 (0)