botserver/src/basic/keywords/math/round.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

use crate::shared::models::UserSession;
use crate::shared::state::AppState;
use log::debug;
use rhai::Engine;
use std::sync::Arc;
pub fn round_keyword(_state: &Arc<AppState>, _user: UserSession, engine: &mut Engine) {
engine.register_fn("ROUND", |n: f64| -> i64 { n.round() as i64 });
engine.register_fn("ROUND", |n: f64, decimals: i64| -> f64 {
let factor = 10_f64.powi(decimals as i32);
(n * factor).round() / factor
});
engine.register_fn("round", |n: f64| -> i64 { n.round() as i64 });
engine.register_fn("round", |n: f64, decimals: i64| -> f64 {
let factor = 10_f64.powi(decimals as i32);
(n * factor).round() / factor
});
debug!("Registered ROUND keyword");
}
#[cfg(test)]
mod tests {
#[test]
fn test_round_basic() {
assert_eq!(3.7_f64.round() as i64, 4);
assert_eq!(3.2_f64.round() as i64, 3);
assert_eq!((-3.7_f64).round() as i64, -4);
}
#[test]
fn test_round_decimals() {
let n = 3.14159_f64;
let decimals = 2;
let factor = 10_f64.powi(decimals);
let result = (n * factor).round() / factor;
assert!((result - 3.14).abs() < 0.001);
}
}