From c147ae843b9f1469926a501e6fd7c3ccdf652ca4 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 9 Nov 2026 08:39:21 +0000 Subject: [PATCH] Show 0 instead of -0 for tiny negative graph labels Found while testing crates/engine/src/cmd/graph/data.rs:166-172 (fmt_value) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/cmd/graph/data.rs | 4 +++- crates/engine/src/cmd/graph/tests.rs | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/graph/data.rs b/crates/engine/src/cmd/graph/data.rs index 913d52522..b49a0d6a8 100644 --- a/crates/engine/src/cmd/graph/data.rs +++ b/crates/engine/src/cmd/graph/data.rs @@ -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 } } } diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index cc986da63..7acfc2c81 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -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(