Roundable

Trait Roundable 

Source
pub trait Roundable: Sized {
    // Required method
    fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>;

    // Provided method
    fn round_to(self, factor: Self, tie: Tie) -> Self { ... }
}
Expand description

Methods to round to an arbitrary factor.

For example, you might wish to round an integer to the nearest ten or nearest hundred:

use roundable::{Roundable, Tie};

assert!(310 == 314.round_to(10, Tie::Up));
assert!(Some(300) == 314.try_round_to(100, Tie::Up));

Required Methods§

Source

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Round to the nearest factor. Returns None if there is an overflow.

Ties (values exactly halfway between to round numbers) are handled according to the second parameter. For traditional rounding use Tie::Up, which will cause ties to be resolved by choosing the higher round number.

See Tie for other tie strategies.

use roundable::{Roundable, Tie};

assert!(Some(315) == 314.try_round_to(5, Tie::Up));
assert!(Some(-10) == (-15).try_round_to(10, Tie::Up));

255u8 can’t be rounded to the nearest 10 (which would be 260) because 260 won’t fit in a u8:

assert!(None == 255u8.try_round_to(10, Tie::Up));
§Panics

Panics if factor is not positive, e.g. if it’s 0.

Provided Methods§

Source

fn round_to(self, factor: Self, tie: Tie) -> Self

Round to the nearest factor. Panics if there is an overflow.

Ties (values exactly halfway between to round numbers) are handled according to the second parameter. For traditional rounding use Tie::Up, which will cause ties to be resolved by choosing the higher round number.

See Tie for other tie strategies.

use roundable::{Roundable, Tie};

assert!(315 == 314.round_to(5, Tie::Up));
assert!(-10 == (-15).round_to(10, Tie::Up));

255u8 can’t be rounded to the nearest 10 (which would be 260) because 260 won’t fit in a u8:

let _ = 255u8.round_to(10u8, Tie::Up);
§Panics

Panics if factor is not positive, e.g. if it’s 0, or if rounding would return a value that does not fit in the return type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Roundable for f32

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for f64

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for i8

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for i16

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for i32

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for i64

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for i128

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for isize

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for u8

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for u16

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for u32

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for u64

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for u128

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for usize

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Source§

impl Roundable for Duration

Source§

fn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>

Implementors§