pub fn get_float_from_string(string: Option<String>) -> Option<f32>
Expand description

Convert optional string to f32, otherwise return None.

Example

use goose::util;

// No decimal returns a proper float.
assert_eq!(util::get_float_from_string(Some("1".to_string())), Some(1.0));

// Leading decimal returns a proper float.
assert_eq!(util::get_float_from_string(Some(".1".to_string())), Some(0.1));

// Valid float string returns a proper float.
assert_eq!(util::get_float_from_string(Some("1.1".to_string())), Some(1.1));

// Invalid number with too many decimals returns None.
assert_eq!(util::get_float_from_string(Some("1.1.1".to_string())), None);

// No number returns None.
assert_eq!(util::get_float_from_string(None), None);