Skip to content

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

M12: FormCalc prints numbers to at most 15 significant digits (#5819)

SSwanson97a911053374
diff --git a/crates/js/src/formcalc.rs b/crates/js/src/formcalc.rs
index 3e68ce081..cfad61993 100644
--- a/crates/js/src/formcalc.rs
+++ b/crates/js/src/formcalc.rs
@@ -227,4 +227,22 @@ mod tests {
assert_eq!(parse_num("12abc"), Some(12.0));
assert_eq!(parse_num("abc"), None);
}
+
+ /// A number prints to at most fifteen significant digits, so the binary noise of a value
+ /// with six digits before the point (279893.88 is 279893.880000000004656… as a double) stays
+ /// hidden, while small values keep their twelve decimals.
+ #[test]
+ fn num_text_prints_at_most_fifteen_significant_digits() {
+ assert_eq!(num_text(279_893.88), "279893.88");
+ assert_eq!(num_text(-279_893.88), "-279893.88");
+ assert_eq!(num_text(279_893.88 + 291_774.12 + 304_171.06 + 317_107.44 + 273_970.85), "1466917.35");
+ assert_eq!(num_text(1_000_000.0 / 3.0), "333333.333333333");
+ assert_eq!(num_text(1.0 / 3.0), "0.333333333333");
+ assert_eq!(num_text(0.3 - 0.1 - 0.2), "0", "noise below twelve decimals is zero");
+ // Past fifteen digits before the point no decimals are left, and the integer keeps its zeros.
+ assert_eq!(num_text(1e15 + 0.5), "1000000000000000");
+ assert_eq!(num_text(1e20), "100000000000000000000");
+ assert_eq!(num_text(-1e20), "-100000000000000000000");
+ assert_eq!(num_text(f64::MAX).len(), 309);
+ }
}
diff --git a/crates/js/src/formcalc/value.rs b/crates/js/src/formcalc/value.rs
index 16705e67e..5ea9d4c08 100644
--- a/crates/js/src/formcalc/value.rs
+++ b/crates/js/src/formcalc/value.rs
@@ -21,7 +21,11 @@ pub(super) enum Val {
Sink,
}
-/// A number as FormCalc prints it: integers without a fraction, otherwise up to 15 digits.
+/// A number as FormCalc prints it: integers without a fraction, otherwise to at most 12
+/// decimals and 15 significant digits, without trailing zeros. 15 digits is the most a double
+/// holds for every value, so a value typed with up to 15 digits prints back as typed
+/// (`279893.88 * 1` is `279893.88`, not `279893.880000000005`), and the binary noise past
+/// them never shows. The 12 decimals hide noise left by sums that should be zero.
pub(super) fn num_text(n: f64) -> String {
if !n.is_finite() {
return if n.is_nan() {
@@ -35,8 +39,12 @@ pub(super) fn num_text(n: f64) -> String {
if n.fract() == 0.0 && n.abs() < 1e15 {
return format!("{}", n as i64);
}
- let s = format!("{n:.12}");
- let s = s.trim_end_matches('0').trim_end_matches('.');
+ // Digits before the point (at most 309 for a finite double); 1 for |n| < 1.
+ let int_digits = format!("{:.0}", n.abs().trunc()).len();
+ let decimals = 15usize.saturating_sub(int_digits).min(12);
+ let s = format!("{n:.decimals$}");
+ // Only a fraction loses its trailing zeros: with no decimals left they belong to the integer.
+ let s = if s.contains('.') { s.trim_end_matches('0').trim_end_matches('.') } else { s.as_str() };
if s.is_empty() || s == "-0" { "0".into() } else { s.to_string() }
}
diff --git a/crates/js/src/tests.rs b/crates/js/src/tests.rs
index 2d3877efe..b61649b72 100644
--- a/crates/js/src/tests.rs
+++ b/crates/js/src/tests.rs
@@ -1026,6 +1026,20 @@ mod formcalc {
assert_eq!(calc(r#"Format("9.9", -1.5)"#), "-1.5");
}
+ /// A calculated amount shows the number it is, not the binary noise past a double's 15
+ /// significant digits: 279893.88 is stored as 279893.880000000004656…, which twelve fixed
+ /// decimals printed as `279893.880000000005`. Small values keep their twelve decimals.
+ #[test]
+ fn calculated_numbers_print_at_most_fifteen_significant_digits() {
+ assert_eq!(calc("1 * 279893.88"), "279893.88");
+ assert_eq!(calc("1 * 291774.12"), "291774.12");
+ assert_eq!(calc("279893.88 + 291774.12 + 304171.06 + 317107.44 + 273970.85"), "1466917.35");
+ assert_eq!(calc("1000000 / 3"), "333333.333333333");
+ assert_eq!(calc("1000 / 3"), "333.333333333333");
+ assert_eq!(calc("1 / 3"), "0.333333333333");
+ assert_eq!(calc("0.1 + 0.2"), "0.3");
+ }
+
/// 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]