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).
import { Decimal } from "@514labs/moose-lib"; interface FinancialData { amount: Decimal<10, 2>; // Decimal(10,2) — up to 99999999.99 rate: Decimal<5, 4>; // Decimal(5,4) — up to 9.9999 fee: Decimal<8, 3>; // Decimal(8,3) — up to 99999.999}You can also use the verbose syntax:
import { ClickHouseDecimal } from "@514labs/moose-lib"; interface FinancialDataVerbose { amount: string & ClickHouseDecimal<10, 2>; rate: string & ClickHouseDecimal<5, 4>;}| 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.