bigdecimal-rs
Arbitrary-precision decimal arithmetic for Rust with exact base-10 rounding
Repository Health
Technical Analysis
bigdecimal-rs provides a BigDecimal type for arbitrary-precision decimal arithmetic in pure Rust, avoiding the rounding errors inherent to binary floating-point when working with base-10 numbers such as currency and financial data. It supports addition, subtraction, multiplication, division, square roots, and configurable precision/rounding contexts, along with optional serde and serde_json integration for lossless serialization of decimal values across systems.
What You Get
- A
BigDecimaltype supporting arithmetic (add, sub, mul, div, sqrt, inverse) with arbitrary precision - Configurable rounding modes and precision via a
Contextstruct, with compile-time environment variable defaults - Multiple
Displayformatting options including scientific notation, fixed precision, and debug representations - Optional
serde/serde-jsonfeature for serializing decimals as strings or as full-precision JSON numbers no_stdsupport via thestdfeature flag for embedded or constrained environments
Common Use Cases
- Representing currency and financial amounts without floating-point rounding drift
- Parsing and formatting decimal numbers from JSON or text input with exact precision preserved
- Scientific or engineering calculations that need arbitrary decimal precision beyond f64
- Interchange of decimal values between systems via serde without losing precision
Under The Hood
Architecture - The crate centers on a BigDecimal struct (src/lib.rs, ~2400 lines) wrapping a BigInt significand plus an i64 scale exponent, with the core value logic split across impl_ops_*.rs files for each arithmetic operator, impl_cmp.rs for comparisons, impl_fmt.rs for Display/Debug, and parsing.rs for string/float parsing; rounding.rs and context.rs implement a separate Context/RoundingMode system that threads through division and sqrt to bound precision for non-terminating results, and impl_serde.rs bridges to serde/serde_json behind feature flags.
Tech Stack - Pure Rust (98.5% of the codebase) targeting edition 2015 for broad compatibility (minimum rustc 1.43), built on num-bigint, num-integer, and num-traits for the underlying big-integer arithmetic, with libm supplying no_std-compatible math functions; build.rs plus autocfg handle compile-time feature detection, and optional serde/serde_json dependencies are gated behind the serde-json Cargo feature.
Code Quality - Tests are embedded directly alongside implementation via included test modules (lib.tests.rs, lib.tests.double.rs, rounding.tests.rs, parsing.tests.parse_from_f32.rs, etc.) totaling 30+ files containing #[test] functions, plus a dedicated property-tests module gated behind a PROPERTY-TESTS feature flag; the crate has no dependency on unsafe code for its core logic and documents formatting/rounding edge cases extensively in both code comments and the README, though the codebase shows some rough edges from an acknowledged in-progress rewrite mentioned in its own README.
API Design - The public API mirrors Rust’s standard numeric traits (Add, Sub, Mul, Div, PartialOrd, FromStr, Display) so BigDecimal behaves like a drop-in numeric type, and configuration is exposed via compile-time environment variables (RUST_BIGDECIMAL_DEFAULT_PRECISION, etc.) for zero-runtime-cost defaults with an escape hatch to explicit Context objects for callers needing per-call control; documentation on docs.rs and the README’s formatting-rules table make the non-obvious scientific-notation thresholds discoverable.
Used by 4 apps in this directory
Fluree DB
Databases
A temporal, verifiable graph database with git-like branching, integrated vector/text/geo search, and RDF/SPARQL/JSON-LD/openCypher support — benchmarked at 10.4x faster than the next database on the full Wikidata dump.
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.
Vaultwarden
Password Manager · Security
Unofficial Bitwarden-compatible server in Rust — run the full Bitwarden ecosystem on a Raspberry Pi using every official client you already have, without the multi-container overhead.
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.