precision_core/
rounding.rs

1//! Rounding modes for decimal arithmetic.
2
3use serde::{Deserialize, Serialize};
4
5/// Rounding mode for decimal operations.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
7pub enum RoundingMode {
8    /// Round toward negative infinity.
9    Down,
10    /// Round toward positive infinity.
11    Up,
12    /// Round toward zero (truncate).
13    TowardZero,
14    /// Round away from zero.
15    AwayFromZero,
16    /// Round to nearest, ties go to even (banker's rounding).
17    #[default]
18    HalfEven,
19    /// Round to nearest, ties round up.
20    HalfUp,
21    /// Round to nearest, ties round down.
22    HalfDown,
23}
24
25impl RoundingMode {
26    pub(crate) fn to_rust_decimal(self) -> rust_decimal::RoundingStrategy {
27        match self {
28            Self::Down => rust_decimal::RoundingStrategy::ToNegativeInfinity,
29            Self::Up => rust_decimal::RoundingStrategy::ToPositiveInfinity,
30            Self::TowardZero => rust_decimal::RoundingStrategy::ToZero,
31            Self::AwayFromZero => rust_decimal::RoundingStrategy::AwayFromZero,
32            Self::HalfEven => rust_decimal::RoundingStrategy::MidpointNearestEven,
33            Self::HalfUp => rust_decimal::RoundingStrategy::MidpointAwayFromZero,
34            Self::HalfDown => rust_decimal::RoundingStrategy::MidpointTowardZero,
35        }
36    }
37}