pub fn exceeds(usd: f64, decimal_places: usize, limit: Option<f64>) -> bool {
let scale = 10f64.powi(decimal_places as i32);
let shown_usd = (usd * scale).round() / scale;
limit.is_some_and(|limit| shown_usd > limit)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_no_limit_never_exceeds() {
assert!(!exceeds(1_000_000.0, 2, None));
}
#[test]
fn test_strictly_above_limit_exceeds() {
assert!(!exceeds(5.0, 2, Some(5.0)));
assert!(exceeds(5.01, 2, Some(5.0)));
}
#[test]
fn test_compares_at_shown_precision() {
assert!(!exceeds(57.0 * 0.01, 2, Some(0.57)));
assert!(!exceeds(5.004, 2, Some(5.0)));
assert!(!exceeds(5.4, 0, Some(5.0)));
}
}