Trait Round

Source
pub trait Round {
    // Required methods
    fn round_to(&mut self, decimal_places: u32) -> f64;
    fn with_significant_figures(&mut self, digits: u64) -> Self;
}
Expand description

Allows the usage of rounding methods that are more specific than rust std’s round() method.

Required Methods§

Source

fn round_to(&mut self, decimal_places: u32) -> f64

Rounds self (a number) to the given number of decimal places. This method is mainly made for the f32 and f64 types since integer types already have no decimal places.

Example:

assert_eq!(23.3274.round_to(2), 23.33);

assert_eq!((1. / 3.).round_to(5), 0.33333);

// For integer types, rounding to a decimal point is the same as casting it to f64
assert_eq!(100_u8.round_to(10), 100.);
Source

fn with_significant_figures(&mut self, digits: u64) -> Self

Rounds self (a number) to the given number of significant figures. Example:

assert_eq!(14912387964_u128.with_significant_figures(5), 14912000000);

assert_eq!(-4095_i32.with_significant_figures(1), -4000);

assert_eq!(1234.5678_f64.with_significant_figures(6), 1234.57)

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 Round for f32

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for f64

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for i8

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for i16

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for i32

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for i64

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for i128

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for isize

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for u8

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for u16

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for u32

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for u64

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for u128

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Source§

impl Round for usize

Source§

fn round_to(&mut self, decimal_places: u32) -> f64

Source§

fn with_significant_figures(&mut self, digits: u64) -> Self

Implementors§