macro_rules! decl_decimal_rounding_methods {
(wide $Type:ident) => {
$crate::macros::rounding_methods::decl_decimal_rounding_methods!(@common $Type);
impl<const SCALE: u32> $Type<SCALE> {
#[inline]
#[must_use]
pub fn round(self) -> Self {
let m = Self::multiplier();
let half = m >> 1u32;
let bias = if self.0.is_negative() { -half } else { half };
Self((self.0 + bias) / m * m)
}
}
};
($Type:ident) => {
$crate::macros::rounding_methods::decl_decimal_rounding_methods!(@common $Type);
impl<const SCALE: u32> $Type<SCALE> {
#[inline]
#[must_use]
pub fn round(self) -> Self {
let m = Self::multiplier();
let half = m / 2;
let bias = if self.0 >= 0 { half } else { -half };
Self((self.0 + bias) / m * m)
}
}
};
(@common $Type:ident) => {
impl<const SCALE: u32> $Type<SCALE> {
#[inline]
#[must_use]
pub fn floor(self) -> Self {
let m = Self::multiplier();
Self(self.0.div_euclid(m) * m)
}
#[inline]
#[must_use]
pub fn ceil(self) -> Self {
let m = Self::multiplier();
Self(-((-self.0).div_euclid(m)) * m)
}
#[inline]
#[must_use]
pub fn trunc(self) -> Self {
let m = Self::multiplier();
Self(self.0 / m * m)
}
#[inline]
#[must_use]
pub fn fract(self) -> Self {
let m = Self::multiplier();
Self(self.0 - (self.0 / m * m))
}
}
};
}
pub(crate) use decl_decimal_rounding_methods;