Decimals provide fixed-point precision, ideal for financial calculations where floating-point errors are unacceptable.
A decimal number with P total digits (precision) and S digits after the decimal point (scale).
from moose_lib import clickhouse_decimal class FinancialData(BaseModel): amount: clickhouse_decimal(10, 2) # Decimal(10,2) rate: clickhouse_decimal(5, 4) # Decimal(5,4) fee: clickhouse_decimal(8, 3) # Decimal(8,3)| Use Case | Precision | Scale | Example Type |
|---|---|---|---|
| Currency (cents) | 10 | 2 | Decimal<10, 2> |
| Currency (4 decimals) | 12 | 4 | Decimal<12, 4> |
| Interest rates | 5 | 4 | Decimal<5, 4> |
| Percentages | 5 | 2 | Decimal<5, 2> |
| Scientific | 18 | 9 | Decimal<18, 9> |
| ClickHouse Type | TypeScript Helper | TypeScript Verbose | Python |
|---|---|---|---|
Decimal(P,S) | Decimal<P, S> | string & ClickHouseDecimal<P, S> | clickhouse_decimal(P, S) |
Use Decimal for monetary values, rates, and any calculation where exact decimal representation matters. Floating-point types (Float32/Float64) can introduce rounding errors in these scenarios.
ClickHouse stores Decimals based on precision:
Choose the smallest precision that fits your needs.