#odd #odds #decimal #fractional #parser #comparing #calculating

wager

Primitive types and functionality for betting odds

2 releases

new 0.1.1 Mar 1, 2025
0.1.0 Feb 24, 2025

#1090 in Rust patterns

Download history 103/week @ 2025-02-19 175/week @ 2025-02-26

278 downloads per month

Apache-2.0

34KB
663 lines

wager

A library for the representation and manipulation of betting odds. Supports the parsing, converting, comparing, and calculating the payouts of various types of odds.

Basic usage

use wager::odd::{Decimal, Fractional, Moneyline, Odd, AnyOdd};

// Parse
let moneyline = "-200".parse::<Moneyline>().unwrap();

// Convert
let decimal = Decimal::try_from(moneyline).unwrap();

// Compare
let a = "+300".parse::<AnyOdd>().unwrap();
let b = "3/1".parse::<AnyOdd>().unwrap();
assert_eq!(a, b);

/// Calculate payout
assert_eq!(decimal.payout(200.0), 300.0);

lib.rs:

A library for the representation and manipulation of betting odds. Includes support for the following types of odds:

Basic usage

Create

use wager::odd::{Decimal, Fractional, Moneyline};

// Fractional odds
let fractional = Fractional::new(1, 2).unwrap();

// Decimal odds
let decimal = Decimal::new(1.5).unwrap();

// Moneyline odds
let moneyline = Moneyline::new(-200).unwrap();

Parse

// Parse odds directly if you know the format ahead of time:
use wager::odd::{Decimal, Fractional, Moneyline, Odd, AnyOdd};

let fractional = "1/2".parse::<Fractional>().unwrap();
let decimal = "1.5".parse::<Decimal>().unwrap();
let moneyline = "-200".parse::<Moneyline>().unwrap();

// Parse odds generically:
match "1/2".parse::<AnyOdd>().unwrap() {
    AnyOdd::Fractional(fractional) => {} // Do something with fractional odd
    AnyOdd::Decimal(decimal) => {} // Do something with decimal odd
    AnyOdd::Moneyline(moneyline) => {} // Do something with moneyline odd
}

Convert

use wager::odd::{Decimal, Fractional, Moneyline};

let fractional = Fractional::new(1, 2).unwrap();
let decimal = Decimal::try_from(fractional).unwrap();
let moneyline = Moneyline::try_from(decimal).unwrap();
It's very important to note that converting between odds is not always exact. For example, converting a decimal odd to a fractional odd requires the approximation of a real number from a rational number.

Calculate payout

use wager::odd::{Decimal, Moneyline, Odd};

let decimal = Decimal::new(1.5).unwrap();
let payout = decimal.payout(100.0);
assert_eq!(payout, 150.0);

let moneyline = Moneyline::new(-200).unwrap();
let payout = moneyline.payout(100.0);
assert_eq!(payout, 150.0);

Compare

use wager::odd::{AnyOdd, Decimal, Moneyline};

// With same type
let a = Decimal::new(1.5).unwrap();
let b = Decimal::new(1.6).unwrap();
assert!(a < b);

// With different types
// Need to wrap in an AnyOdd first
let a = AnyOdd::Decimal(Decimal::new(1.4).unwrap());
let b = AnyOdd::Moneyline(Moneyline::new(-200).unwrap());
assert!(a < b);

Dependencies

~0.3–0.9MB
~20K SLoC