Trait stdext::num::float_convert::FloatConvert
source · pub trait FloatConvert<Int>: Sized {
// Required methods
fn checked_floor(self) -> Option<Int>;
fn checked_ceil(self) -> Option<Int>;
fn checked_round(self) -> Option<Int>;
fn saturated_floor(self) -> Int;
fn saturated_ceil(self) -> Int;
fn saturated_round(self) -> Int;
}
Expand description
Set of methods to safely convert floating number into an integer.
Currently, the main way to do so is to use as
conversion.
However, such an approach may not be suitable if saturating conversion is
not desired.
However, saturating conversion is also provided as an expicit alternative
to as
conversion (e.g. to avoid warnings when clippy::as_conversions
lint is enabled).
§Implementors
Required Methods§
sourcefn checked_floor(self) -> Option<Int>
fn checked_floor(self) -> Option<Int>
Floors the floating number and attempts to convert it into an integer.
See f32::floor
for description of flooring logic.
Returns None
if the value will not fit into the integer range or value
is not a number.
§Examples
let valid: Option<u8> = 10.5f32.checked_floor();
let too_big: Option<u8> = 256f32.checked_floor();
let nan: Option<u8> = f32::NAN.checked_floor();
assert_eq!(valid, Some(10u8));
assert_eq!(too_big, None);
assert_eq!(nan, None);
sourcefn checked_ceil(self) -> Option<Int>
fn checked_ceil(self) -> Option<Int>
Ceils the floating number and attempts to convert it into an integer.
See f32::ceil
for description of ceiling logic.
Returns None
if the value will not fit into the integer range or value
is not a number.
§Examples
let valid: Option<u8> = 10.5f32.checked_ceil();
let too_big: Option<u8> = 256f32.checked_ceil();
let nan: Option<u8> = f32::NAN.checked_ceil();
assert_eq!(valid, Some(11u8));
assert_eq!(too_big, None);
assert_eq!(nan, None);
sourcefn checked_round(self) -> Option<Int>
fn checked_round(self) -> Option<Int>
Rounds the floating number and attempts to convert it into an integer.
See f32::round
for description of rounding logic.
Returns None
if the value will not fit into the integer range or value
is not a number.
§Examples
let valid: Option<u8> = 10.51f32.checked_round(); // Will be rounded up.
let too_big: Option<u8> = 256f32.checked_round();
let nan: Option<u8> = f32::NAN.checked_round();
assert_eq!(valid, Some(11u8));
assert_eq!(too_big, None);
assert_eq!(nan, None);
sourcefn saturated_floor(self) -> Int
fn saturated_floor(self) -> Int
Behaves the same as number.floor() as <type>
.
See f32::floor
for description of flooring logic.
sourcefn saturated_ceil(self) -> Int
fn saturated_ceil(self) -> Int
Behaves the same as number.ceil() as <type>
.
See f32::ceil
for description of flooring logic.
sourcefn saturated_round(self) -> Int
fn saturated_round(self) -> Int
Behaves the same as number.round() as <type>
.
See f32::round
for description of flooring logic.