Fixed-point decimal arithmetic with compile-time precision.
nexus-decimal provides [Decimal<B, DECIMALS>] — a generic
fixed-point type parameterized by backing integer and decimal
places. Operations are const fn where possible, zero-allocation,
and designed for financial workloads.
Choosing Your Type
Define aliases that match your domain:
use Decimal;
type Price = ; // 8dp, range ±92B — traditional finance
type Quantity = ; // 4dp, range ±922T
type CryptoPrice = ; // 12dp, range ±39T — DeFi
type Usd = ; // 2dp cents
| Backing | Max Decimals | Max Range | Use case |
|---|---|---|---|
i32 |
9 | ±2.1B / SCALE | Embedded, space-constrained |
i64 |
18 | ±9.2e18 / SCALE | Traditional finance |
i128 |
38 | ±1.7e38 / SCALE | Cryptocurrency, DeFi |
Quick Start
use Decimal;
use FromStr;
type D64 = ;
let price = D64from_str.unwrap;
let qty = D64from_i32.unwrap;
let notional = price * qty;
assert_eq!;
let bid = D64from_str.unwrap;
let ask = D64from_str.unwrap;
let mid = bid.midpoint;
assert_eq!;
Compile-Time Constants
use Decimal;
type D64 = ;
const PRICE: D64 = D64new; // 100.50
const FEE: D64 = D64from_raw; // 0.005
const TOTAL: D64 = match PRICE.checked_add ;
Arithmetic Variants
Every arithmetic operation comes in four flavors:
| Variant | Returns | On overflow |
|---|---|---|
checked_* |
Option<Self> |
None |
try_* |
Result<Self, SpecificError> |
Typed error |
saturating_* |
Self |
Clamps to MIN/MAX |
wrapping_* |
Self |
Wraps around |
Operators (+, -, *, /, %) always panic on overflow
in both debug and release builds.
Error Types
Errors are scoped per operation — no catch-all enum:
| Error | Used by | Variants |
|---|---|---|
[OverflowError] |
try_add, try_mul, etc. |
(unit struct) |
[DivError] |
try_div |
Overflow, DivisionByZero |
[ParseError] |
from_str_exact, FromStr |
InvalidFormat, Overflow, PrecisionLoss |
[ConvertError] |
from_f64, TryFrom |
Overflow, PrecisionLoss |
Feature Flags
| Feature | Dependencies | Provides |
|---|---|---|
std (default) |
— | Error trait impls |
serde |
serde |
Serialize/Deserialize (string for JSON, raw for binary) |
num-traits |
num-traits |
Zero, One, Num, Signed, Bounded, Checked*, ToPrimitive |
no_std Support
Disable default features for no_std:
= { = "0.1", = false }
Migration from fixdec
// Before:
use fixdec::D64;
// After:
use nexus_decimal::Decimal;
type D64 = Decimal<i64, 8>;
API differences:
mul_i64/mul_i128→mul_int(takes the backing type)DecimalError→ per-method error types ([OverflowError], [DivError], etc.)- No predefined aliases — define your own (
type Price = Decimal<i64, 8>) - New: financial methods (
midpoint,spread,round_to_tick, etc.)