Omit minus sign when FormCalc Format() rounds to zero

Found while testing crates/js/src/formcalc.rs:2328-2330 ("format" number-picture branch, `if num < 0.0 { text = format!("-{text}") }`)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-23 20:53:02 +02:00
co-authored by Claude Haiku 5.5
parent aea5e78e66
commit afcc54fddc
2 changed files with 10 additions and 1 deletions
+2 -1
View File
@@ -2325,10 +2325,11 @@ impl Interp<'_> {
}
text = if frac.is_empty() { g } else { format!("{g}.{frac}") };
}
let nonzero = text.chars().any(|c| ('1'..='9').contains(&c));
if pic.contains('$') {
text = format!("${text}");
}
if num < 0.0 {
if num < 0.0 && nonzero {
text = format!("-{text}");
}
Val::Str(text)
+8
View File
@@ -805,6 +805,14 @@ mod formcalc {
o.result.unwrap_or_default()
}
/// A negative value that rounds to zero under the picture prints without a minus sign.
#[test]
fn format_does_not_print_negative_zero() {
assert_eq!(calc(r#"Format("9.9", -0.04)"#), "0.0");
assert_eq!(calc(r#"Format("99", -0.2)"#), "00");
assert_eq!(calc(r#"Format("9.9", -1.5)"#), "-1.5");
}
/// Assigning one long value to many fields at once stops at the run's byte budget and is
/// charged per copy, and a reset list names each field once.
#[test]