Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Omit minus sign when FormCalc Format() rounds to zero

NetterSilverafcc54fddc6e
diff --git a/crates/js/src/formcalc.rs b/crates/js/src/formcalc.rs
index 2348ce220..50191f1f0 100644
--- a/crates/js/src/formcalc.rs
+++ b/crates/js/src/formcalc.rs
@@ -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)
diff --git a/crates/js/src/tests.rs b/crates/js/src/tests.rs
index b2e866fae..c7419be47 100644
--- a/crates/js/src/tests.rs
+++ b/crates/js/src/tests.rs
@@ -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]