Expand description
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);
Modules§
- odd
- Odds functionality and primitives.