finmoney
A precise, panic-free FinMoney library for Rust. finmoney provides safe monetary arithmetic, currency-aware values, configurable rounding strategies, and exchange-grade tick handling. Designed for trading systems, bots, and financial apps where correctness and determinism matter.
Features
- Precise arithmetic: Built on
rust_decimalfor exact decimal calculations - Currency safety: Prevents mixing different currencies in operations
- Configurable rounding: Multiple rounding strategies for different use cases
- Tick handling: Exchange-grade price/quantity rounding to valid tick sizes
- Zero panics: All operations return
Resulttypes for error handling - Serde support: Optional serialization/deserialization (feature-gated)
- Modern Rust: Uses Rust 2024 edition for the latest language features
Requirements
- Rust 1.90 or later (Rust 2024 edition)
Rust 2024 Edition Benefits
This library uses the Rust 2024 edition, which provides:
- Improved error messages and diagnostics
- Better async/await ergonomics
- Enhanced pattern matching capabilities
- More consistent and intuitive syntax
- Latest language features and optimizations
Quick Start
Add this to your Cargo.toml:
[]
= "1.0.2"
# For serialization support
= { = "1.0.2", = ["serde"] }
Basic Usage
use ;
use dec;
// Create currencies
let usd = new?;
let btc = new?;
// Or use predefined currencies
let usd = USD;
let eur = EUR;
// Create FinMoney values
let price = new;
let tax = new;
// Perform arithmetic (returns Result for safety)
let total = ?;
println!; // 11.55 USD
// Multiply by decimal
let doubled = price * dec!;
println!; // 21.00 USD
// Division with rounding
let divided = price.divided_by_decimal?;
println!; // 3.50 USD
Currency Safety
finmoney prevents mixing different currencies:
let usd_amount = new;
let eur_amount = new;
// This will return an error
match usd_amount + eur_amount
Tick Handling for Trading
Perfect for exchange trading where prices must conform to specific tick sizes:
let price = new;
// Round to nearest 0.25 tick
let rounded = price.to_tick_nearest?;
println!; // 10.50 USD
// Round down (floor)
let floor_price = price.to_tick_down?;
println!; // 10.50 USD
// Round up (ceiling)
let ceil_price = price.to_tick_up?;
println!; // 10.75 USD
// Check if price is valid for tick size
if price.is_multiple_of_tick
Rounding Strategies
Multiple rounding strategies are available:
let amount = new;
// Banker's rounding (default)
let rounded1 = amount.round_dp_with_strategy;
// Always round away from zero
let rounded2 = amount.round_dp_with_strategy;
// Always round toward zero
let rounded3 = amount.round_dp_with_strategy;
Percentage Calculations
let initial = new;
let current = new;
// Calculate percentage change
let change = current.percent_change_from?;
println!; // Change: 10%
// Or use static method
let change = percent_change?;
Comparison Operations
let price1 = new;
let price2 = new;
// Safe comparisons (returns Result)
if price1.is_greater_than?
// Min/max operations
let lower = price1.min?;
let higher = price1.max?;
// Direct decimal comparisons (no Result needed)
if price1.is_greater_than_decimal
Properties and Utilities
let FinMoney = new;
println!;
println!;
println!;
println!;
println!;
// Mathematical operations
let abs_FinMoney = FinMoney.abs; // 15.75 USD
let neg_FinMoney = FinMoney.negated; // 15.75 USD
let floor_FinMoney = FinMoney.floor; // -16.00 USD
let ceil_FinMoney = FinMoney.ceil; // -15.00 USD
Error Handling
All operations that can fail return Result<T, MoneyError>:
use MoneyError;
let result = FinMoney1.divided_by_decimal;
match result
Predefined Currencies
Common currencies are available as constants:
let usd_FinMoney = new; // 2 decimal places
let eur_FinMoney = new; // 2 decimal places
let btc_FinMoney = new; // 8 decimal places
let eth_FinMoney = new; // 18 decimal places
Serde Support
Enable the serde feature for serialization support:
[]
= { = "0.1", = ["serde"] }
use ;
let order = Order ;
let json = to_string?;
let deserialized: Order = from_str?;
Performance
finmoney is built on rust_decimal which provides excellent performance for financial calculations. All operations are designed to be allocation-free where possible.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.