Merge pull request #6294 from Silvertree2010/fix/h14198-graph-labels-show-0-for-tiny-neg

Show 0 instead of -0 for tiny negative graph labels
This commit is contained in:
BFlat
2026-11-09 17:29:15 +08:00
committed by GitHub
2 changed files with 10 additions and 1 deletions
+3 -1
View File
@@ -169,7 +169,9 @@ pub(super) fn fmt_value(v: f64) -> String {
// `{:.0}` prints its exact digits.
if v.abs() < 9e18 { format!("{}", v.round() as i64) } else { format!("{v:.0}") }
} else {
format!("{:.2}", v).trim_end_matches('0').trim_end_matches('.').to_string()
let s = format!("{:.2}", v).trim_end_matches('0').trim_end_matches('.').to_string();
// A tiny negative rounds to "-0.00"; show it as plain zero like -0.0.
if s == "-0" { "0".to_string() } else { s }
}
}
+7
View File
@@ -14,6 +14,13 @@ fn fmt_value_does_not_saturate_beyond_i64() {
assert_eq!(super::fmt_value(2.5), "2.5");
}
#[test]
fn fmt_value_tiny_negative_is_zero() {
assert_eq!(super::fmt_value(-0.004), "0");
assert_eq!(super::fmt_value(-0.0049), "0");
assert_eq!(super::fmt_value(-0.006), "-0.01");
}
fn graph(s: &mut Session, ty: &str) -> NodeId {
let csv = ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6";
NodeId(