Odds Converter
A Rust library for converting between different betting odds formats with robust error handling and comprehensive validation.
Features
- Multiple Formats: Support for American, Decimal, and Fractional odds
- Bidirectional Conversion: Convert between any two formats
- Implied Probability: Calculate implied probabilities from odds
- String Parsing: Parse odds from common string representations
- Robust Validation: Comprehensive input validation and error handling
- Minimal Dependencies: Pure Rust implementation with minimal dependencies
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Conversions
use Odds;
// Create odds in different formats
let american = new_american; // +150 American odds
let decimal = new_decimal; // 2.50 decimal odds
let fractional = new_fractional; // 3/2 fractional odds
// Convert between formats
assert_eq!;
assert_eq!;
assert_eq!;
// Calculate implied probability
assert_eq!; // 40%
String Parsing
use Odds;
// Parse from strings
let odds1: Odds = "+150".parse.unwrap; // American format
let odds2: Odds = "2.50".parse.unwrap; // Decimal format
let odds3: Odds = "3/2".parse.unwrap; // Fractional format
// Display as strings
println!; // "+150"
println!; // "2.50"
println!; // "3/2"
Error Handling
use ;
// Validation catches invalid odds
let invalid = new_decimal;
match invalid.validate
// Parsing handles malformed input
let result: = "invalid".parse;
assert!;
Real-World Examples
use Odds;
// Convert Vegas odds to European format
let vegas_line = new_american;
let european_odds = vegas_line.to_decimal.unwrap;
println!; // 1.91
// Calculate probability for UK fractional odds
let uk_odds = new_fractional;
let probability = uk_odds.implied_probability.unwrap;
println!; // 30.8%
// Parse user input and convert
let user_input = "+200";
let odds: Odds = user_input.parse.unwrap;
let decimal_equivalent = odds.to_decimal.unwrap;
println!;
Edge Case Handling
The library automatically handles edge cases in American odds between -99 to +99 (excluding zero):
use Odds;
// Positive odds 1-99 are normalized to equivalent negative odds
let odds_pos = new_american; // Automatically becomes -200
assert_eq!;
// Negative odds -1 to -99 are normalized to equivalent positive odds
let odds_neg = new_american; // Automatically becomes +200
assert_eq!;
// This ensures conversions never produce invalid odds in the problematic range
let decimal = new_decimal;
let american = decimal.to_american.unwrap; // Returns -400, not +25
Odds Formats
American Odds (Moneyline)
- Positive numbers: Profit on a $100 bet (e.g., +150 = $150 profit)
- Negative numbers: Amount to bet to win $100 (e.g., -200 = bet $200 to win $100)
- Edge case handling: Values between -99 to +99 (excluding zero) are automatically normalized to their standard representation
- Common in: United States
Decimal Odds (European)
- Format: Total return including stake (e.g., 2.50 = $2.50 return on $1 bet)
- Range: Always ≥ 1.0
- Common in: Europe, Australia, Canada
Fractional Odds (UK)
- Format: Profit ratio as fraction (e.g., 3/2 = win $3 for every $2 bet)
- Notation: numerator/denominator
- Common in: United Kingdom, Ireland
API Reference
Core Types
Odds- Main struct for holding odds in any formatOddsFormat- Enum representing the three odds formatsOddsError- Error types for validation and parsing failures
Methods
new_american(value: i32)- Create American oddsnew_decimal(value: f64)- Create decimal oddsnew_fractional(num: u32, den: u32)- Create fractional oddsto_american()- Convert to American formatto_decimal()- Convert to decimal formatto_fractional()- Convert to fractional formatimplied_probability()- Calculate implied probabilityvalidate()- Validate odds valuesformat()- Get underlying format
String Operations
parse()- Parse from string (viaFromStrtrait)to_string()- Format as string (viaDisplaytrait)
Mathematical Accuracy
The library handles floating-point precision carefully and includes comprehensive tests for mathematical correctness:
- Property-based testing ensures conversion accuracy
- Round-trip conversions maintain precision within reasonable tolerances
- Edge cases are properly handled (e.g., very small/large odds)
Error Handling
Comprehensive error types provide detailed information about failures:
InvalidAmericanOdds- Zero, -100, or out-of-range American oddsInvalidDecimalOdds- Less than 1.0, infinite, or NaN decimal oddsInvalidFractionalOdds- Invalid fractional valuesZeroDenominator- Division by zero in fractionsParseError- Malformed string inputValueOutOfRange- Unreasonably large values
Performance
Conversions are highly optimized with minimal allocations:
- Typical conversion time: ~1.6-2.0 nanoseconds
- Zero-allocation for numeric conversions
- Efficient string parsing with detailed error messages
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.