rust_decimal
Pure-Rust fixed-precision Decimal type for financial and monetary calculations
Repository Health
Technical Analysis
rust_decimal implements a Decimal number type in pure Rust suitable for financial and fixed-precision calculations that require exact integral and fractional digits with no floating-point round-off error. It uses a 96-bit integer plus a scaling factor and sign bit representation (matching .NET’s System.Decimal layout), preserving trailing zeros and supporting up to 28-29 significant digits.
The crate offers a broad set of optional integrations — PostgreSQL and MySQL database drivers, Diesel ORM support, serde with multiple serialization strategies (string, float, arbitrary precision), no_std/alloc compatibility, ndarray arithmetic, and a compile-time dec! macro — making it a common building block anywhere Rust code needs exact decimal arithmetic (pricing, accounting, currency).
What You Get
- A
Decimaltype with exact fixed-precision arithmetic (add, subtract, multiply, divide, rounding) and no floating-point round-off error - The
dec!compile-time macro for ergonomic, validated decimal literals (dec!(25.12)) - Database driver integrations for PostgreSQL (
db-postgres,db-tokio-postgres), MySQL, and Diesel ORM (db-diesel-postgres,db-diesel-mysql) - Multiple serde serialization strategies — as string, float, or arbitrary precision — configurable per-field via feature flags
- Optional
mathsfeature forpow,ln,exp,log10and other transcendental operations via theMathematicalOpstrait no_std+allocsupport, plusrkyv,borsh,bytemuck,proptest, andrandintegrations
Common Use Cases
- Representing monetary amounts and prices in e-commerce, billing, and accounting systems without float rounding errors
- Storing and querying exact decimal values in PostgreSQL/MySQL applications via the built-in database driver integrations
- Serializing decimal values to JSON/other formats with precise control over string vs. float representation
- Performing scientific or engineering fixed-precision arithmetic that needs more digits of precision than IEEE floats provide
Under The Hood
Architecture — The core Decimal type and its arithmetic are implemented in src/decimal.rs and src/ops/ (separate modules per operation), with src/maths.rs housing the optional transcendental-function feature, and dedicated integration modules (postgres.rs, mysql.rs, serde.rs, borsh.rs, wasm.rs, rand_0_9.rs/rand_0_10.rs) gated behind Cargo feature flags so consumers only compile in what they use. A separate macros crate implements the dec! compile-time literal macro as a proc-macro, and build.rs handles any platform-specific codegen.
Tech Stack — Nearly pure Rust (99.95% of the codebase) with an extensive feature-flag matrix (align16, alloc, borsh, bytemuck, c-repr, macros, maths, ndarray, rkyv, serde-float/serde-str/serde-arbitrary-precision, db-postgres/db-tokio-postgres/db-diesel-postgres/db-diesel-mysql) that lets the crate serve everything from no_std embedded contexts to full ORM-backed financial applications from a single dependency.
Code Quality — tests/decimal_tests plus dedicated test files per integration (postgres_tests.rs, serde_tests.rs, borsh_tests.rs, ndarray_tests.rs, proptest_tests.rs, rust_fuzz_tests.rs) give each feature its own coverage, and a fuzz/ directory plus rust-fuzz/proptest feature flags indicate active property-based and fuzz testing of the arithmetic core — appropriate rigor for a type used in monetary calculations where correctness is critical. The README explicitly documents that the main branch is unstable/pre-2.0 with breaking changes expected, while a maintained v1 branch stays backwards-compatible.
API Design — The dec!(1.23) macro and Decimal::from_str/Decimal::new constructors give multiple ergonomic entry points depending on whether values are known at compile time or come from parsed input, and operator overloading (+, -, *, /) lets Decimal values be used like any other numeric type. The tradeoff of the crate’s large feature-flag surface is that discovering the right combination of flags for a given integration (e.g. serde-with-str vs. serde-str vs. serde-float) requires reading fairly detailed documentation.
Used by 8 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
Arroyo
Data Engineering · Analytics
A distributed stream processing engine written in Rust that lets you write SQL to run stateful, real-time computations over data streams with subsecond results.
InfluxDB
Databases · Analytics
Open-source time-series database built for real-time ingest, fast SQL queries, and embedded Python automation — powered by Apache Arrow and Parquet.
ParadeDB
Search · Databases · Analytics
Born out of Y Combinator's S2023 batch, ParadeDB is a Postgres extension that delivers Elasticsearch-quality BM25 search and real-time analytics without a separate search cluster to manage.
PostHog
Analytics · Monitoring · Developer Tools
The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.
QuestDB
Databases · Analytics
A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.
Windmill
Automation · Developer Tools
Turn scripts into webhooks, workflows, and auto-generated UIs — the fastest self-hostable workflow engine, 13x faster than Airflow.
WrenAI
Analytics · AI Agents · Data Engineering
Open-source GenBI engine that lets AI agents turn natural-language questions into governed SQL, charts, and shareable dashboards across 20+ data sources — no vendor lock-in, no black-box prompts.