enh(output): use a helper for decimal arbitrary precision

This commit is contained in:
evan Adam 2025-06-13 18:51:16 +02:00 committed by David Boucher
parent 59c9ec1512
commit 1bbc23702b
2 changed files with 35 additions and 5 deletions

View File

@ -316,7 +316,7 @@ impl ExprResult {
*s = format!("{}{}", s, ss);
}
ExprResult::Number(n) => {
*s = format!("{}{}", s, n);
*s = format!("{}{:.2}", s, crate::output::float_string(n));
}
_ => panic!("Unable to join objects others than strings"),
},

View File

@ -101,17 +101,17 @@ impl<'a> OutputFormatter<'a> {
format!(
"{}={};{};{};{};{}",
m.name,
m.value,
float_string(&m.value),
m.warning.unwrap_or(""),
m.critical.unwrap_or(""),
match m.min {
Some(min) => min.to_string(),
Some(min) => float_string(&min),
None => "".to_string(),
},
match m.max {
Some(max) => max.to_string(),
Some(max) => float_string(&max),
None => "".to_string(),
},
}
)
})
.collect::<Vec<String>>()
@ -185,3 +185,33 @@ impl<'a> OutputFormatter<'a> {
std::format!("{}{}", prefix, "blabla")
}
}
pub fn float_string(val: &f64) -> String {
let mut s = format!("{:.2}", val);
while s.ends_with('0') {
s.pop();
}
if s.ends_with('.') {
s.pop();
}
s
}
mod test {
#[test]
fn test_float_string() {
use super::float_string;
let f = f64::default();
assert_eq!(float_string(&40.0), "40");
assert_eq!(float_string(&40.00), "40");
assert_eq!(float_string(&40.001), "40");
assert_eq!(float_string(&40.009), "40.01");
assert_eq!(float_string(&40.01), "40.01");
assert_eq!(float_string(&40.104), "40.1");
assert_eq!(float_string(&f), "0");
assert_eq!(float_string(&9999999.999), "10000000");
}
}