pub trait Round
{
fn round_mag(&self, magnitude: i16) -> Self;
fn round_sig(&self, significants: u8) -> Self;
}
impl Round for f64 {
fn round_mag(&self, magnitude: i16) -> Self
{
let x_rounded: Self;
if *self == 0 as Self
{
return 0 as Self;
}
x_rounded = (*self * Self::powi(10 as Self, (-magnitude).into())).round_ties_even() * Self::powi(10 as Self, magnitude.into());
return x_rounded;
}
fn round_sig(&self, significants: u8) -> Self
{
let magnitude: i16;
let x_rounded: Self;
if *self == 0 as Self || significants == 0
{
return 0 as Self;
}
magnitude = self.abs().log10().floor() as i16; x_rounded = self.round_mag(magnitude - i16::from(significants) + 1);
return x_rounded;
}
}