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§
Sourcefn try_round_to(self, factor: Self, tie: Tie) -> Option<Self>
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§
Sourcefn round_to(self, factor: Self, tie: Tie) -> Self
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.