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

This trait is implemented for both f32 and f64.

Required Methods§

source

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);
source

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);
source

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);
source

fn saturated_floor(self) -> Int

Behaves the same as number.floor() as <type>. See f32::floor for description of flooring logic.

source

fn saturated_ceil(self) -> Int

Behaves the same as number.ceil() as <type>. See f32::ceil for description of flooring logic.

source

fn saturated_round(self) -> Int

Behaves the same as number.round() as <type>. See f32::round for description of flooring logic.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FloatConvert<i8> for f32

source§

impl FloatConvert<i8> for f64

source§

impl FloatConvert<i16> for f32

source§

impl FloatConvert<i16> for f64

source§

impl FloatConvert<i32> for f32

source§

impl FloatConvert<i32> for f64

source§

impl FloatConvert<i64> for f32

source§

impl FloatConvert<i64> for f64

source§

impl FloatConvert<i128> for f32

source§

impl FloatConvert<i128> for f64

source§

impl FloatConvert<isize> for f32

source§

impl FloatConvert<isize> for f64

source§

impl FloatConvert<u8> for f32

source§

impl FloatConvert<u8> for f64

source§

impl FloatConvert<u16> for f32

source§

impl FloatConvert<u16> for f64

source§

impl FloatConvert<u32> for f32

source§

impl FloatConvert<u32> for f64

source§

impl FloatConvert<u64> for f32

source§

impl FloatConvert<u64> for f64

source§

impl FloatConvert<u128> for f32

source§

impl FloatConvert<u128> for f64

source§

impl FloatConvert<usize> for f32

source§

impl FloatConvert<usize> for f64

Implementors§