1pub fn round_2_decimals(value: f32) -> f32 {
20 (value * 100.0).round() / 100.0
21}
22
23use std::any::{Any, TypeId};
24use std::str::FromStr;
25
26pub fn check_converted_value<T: Any + FromStr>(
27 result: &Result<T, T::Err>,
28 expected_type: TypeId,
29) -> bool {
30 match result {
31 Ok(value) => value.type_id() == expected_type,
32 Err(_) => false,
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn test_round_2_decimals() {
42 assert_eq!(round_2_decimals(123.4567), 123.46);
43 assert_eq!(round_2_decimals(123.451), 123.45);
44 assert_eq!(round_2_decimals(123.455), 123.46);
45 assert_eq!(round_2_decimals(123.454), 123.45);
46 }
47
48 #[test]
49 fn test_check_converted_value() {
50 let result_ok: Result<f32, _> = "123.45".parse::<f32>();
51 let result_err: Result<f32, _> = "abc".parse::<f32>();
52 assert!(check_converted_value(&result_ok, TypeId::of::<f32>()));
53 assert!(!check_converted_value(&result_err, TypeId::of::<f32>()));
54 }
55}