pub struct FriendlyProbability { /* private fields */ }
Expand description
A probability that can be expressed in a user-friendly form.
The friendly_string
property is a textual representation of
the probability (i.e. “5 out of 7”)
Implementations§
Source§impl FriendlyProbability
impl FriendlyProbability
Sourcepub fn new<T: Into<Option<String>>>(
numerator: u8,
denominator: u8,
friendly_description: &'static str,
friendly_string: T,
) -> FriendlyProbability
pub fn new<T: Into<Option<String>>>( numerator: u8, denominator: u8, friendly_description: &'static str, friendly_string: T, ) -> FriendlyProbability
Create a new FriendlyProbability. Most of the time you will want to use from_probability()
instead.
Sourcepub fn numerator(self: &FriendlyProbability) -> u8
pub fn numerator(self: &FriendlyProbability) -> u8
Gets the numerator of the FriendlyProbability.
Sourcepub fn denominator(self: &FriendlyProbability) -> u8
pub fn denominator(self: &FriendlyProbability) -> u8
Gets the denominator of the FriendlyProbability.
Sourcepub fn friendly_description(self: &FriendlyProbability) -> &str
pub fn friendly_description(self: &FriendlyProbability) -> &str
Gets the friendly description of the FriendlyProbability. This is a qualitative description of the probability (“Still possible”, “Flip a coin”, “Good chance”, etc.)
Sourcepub fn friendly_string(self: &FriendlyProbability) -> &str
pub fn friendly_string(self: &FriendlyProbability) -> &str
Gets the friendly string of the FriendlyProbability. Usually this is the same as “{numerator} in {denominator}”, but if the probability is very small it will instead be “<1 in 100”, and if it’s very large it will be “>99 in 100”
Sourcepub fn from_probability(probability: f32) -> FriendlyProbability
pub fn from_probability(probability: f32) -> FriendlyProbability
Create a FriendlyProbability from an f32.
§Examples
use probability_to_friendly_string::FriendlyProbability;
let friendly = FriendlyProbability::from_probability(0.723);
assert_eq!(5, friendly.numerator());
assert_eq!(7, friendly.denominator());
assert_eq!("Good chance", friendly.friendly_description());
assert_eq!("5 in 7", friendly.friendly_string());
let friendly = FriendlyProbability::from_probability(0.999);
assert_eq!(">99 in 100", friendly.friendly_string());
let friendly = FriendlyProbability::from_probability(0.001);
assert_eq!("<1 in 100", friendly.friendly_string());
§Panics
If probability is less than 0.0 or greater than 1.0.