From 263826852c2c6e6d42a8ee5cd3095d2168e35e20 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Fri, 27 Mar 2026 00:22:08 +0100 Subject: [PATCH 1/3] [add] exception when rounding "0" to figures Always return "0" without fractional --- src/rounding.typ | 13 ++++++++----- tests/rounding/test.typ | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/rounding.typ b/src/rounding.typ index 43c45ac..8cd4428 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -98,15 +98,18 @@ /// /// The result is never negative. #let get-rounding-digit(int, frac, mode, precision) = { - let round-digit = precision + if mode == "places" { - int.len() + if mode == "places" { + precision + int.len() } else if mode == "figures" { - count-leading-zeros(int + frac) + if (int + frac).trim("0") == "" { + int.len() + } else { + precision + count-leading-zeros(int + frac) + } } - - calc.max(round-digit) } + /// Pads a decimal number to given precision with given rounding mode. /// Examples with `pad: true`: /// - `62.1` (`"62", "1"`), `mode: "places", precision: 2` diff --git a/tests/rounding/test.typ b/tests/rounding/test.typ index ecc6265..e4e85af 100644 --- a/tests/rounding/test.typ +++ b/tests/rounding/test.typ @@ -258,3 +258,22 @@ ), ("1", "23", (("", "04"), ("", "30"))), ) +#assert.eq( + round( + "0", + "", + precision: 2, + mode: "places", + ), + ("", "00", none), +) + +#assert.eq( + round( + "0", + "", + precision: 2, + mode: "figures", + ), + ("", "", none), +) From a6204e58aec84b398d95023493272bc614ee8344 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Fri, 27 Mar 2026 09:44:51 +0100 Subject: [PATCH 2/3] [change] Add no exponent with sci/eng if number is 0 --- src/num.typ | 6 ++++++ tests/num/engineering/ref.typ | 7 ++++++- tests/num/engineering/test.typ | 5 +++++ tests/num/scientific/ref.typ | 8 +++++++- tests/num/scientific/test.typ | 6 ++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/num.typ b/src/num.typ index 02a51c5..963338d 100644 --- a/src/num.typ +++ b/src/num.typ @@ -34,6 +34,12 @@ // Process the exponent with its various modes mode #let process-exponent(info, exponent) = { + + if (info.int + info.frac).trim("0") == "" { + // Add no exponent if number is 0 + return info + } + let new-exponent = if type(exponent) == dictionary { assert( "fixed" in exponent or "sci" in exponent, diff --git a/tests/num/engineering/ref.typ b/tests/num/engineering/ref.typ index eb8ce38..83ff46f 100644 --- a/tests/num/engineering/ref.typ +++ b/tests/num/engineering/ref.typ @@ -15,4 +15,9 @@ $10×10^(-3)$ \ $1×10^(-3)$ \ $100×10^(-6)$ \ $10×10^(-6)$ \ -$1×10^(-6)$ \ \ No newline at end of file +$1×10^(-6)$ \ + +#pagebreak() + +$0$ \ +$0×10^2$ \ diff --git a/tests/num/engineering/test.typ b/tests/num/engineering/test.typ index 4501f99..bdbe3a8 100644 --- a/tests/num/engineering/test.typ +++ b/tests/num/engineering/test.typ @@ -19,3 +19,8 @@ #num(exponent: "eng")[.0001] \ #num(exponent: "eng")[.00001] \ #num(exponent: "eng")[1e-6] \ + +#pagebreak() + +#num(exponent: "eng")[0] \ +#num(exponent: "eng")[0e2] \ // it's okay that the exponent is now 2 diff --git a/tests/num/scientific/ref.typ b/tests/num/scientific/ref.typ index 07ebc80..cfa4caf 100644 --- a/tests/num/scientific/ref.typ +++ b/tests/num/scientific/ref.typ @@ -1,7 +1,7 @@ #set page(width: auto, height: auto, margin: .5em) $1.23×10^2$ \ -$1.2×10^11$ +$1.2×10^11$ \ #pagebreak() @@ -16,3 +16,9 @@ $120$ \ $1.200×10^3$ \ $0.001$ \ $1×10^(-4)$ \ + +#pagebreak() + +$0$ \ +$0×10^4$ \ +$0.00$ \ diff --git a/tests/num/scientific/test.typ b/tests/num/scientific/test.typ index 7817e7d..6f95222 100644 --- a/tests/num/scientific/test.typ +++ b/tests/num/scientific/test.typ @@ -19,3 +19,9 @@ #num(exponent: (sci: (-4, 3)))[1200] \ #num(exponent: (sci: (-4, 3)))[.001] \ #num(exponent: (sci: (-4, 3)))[.0001] \ + +#pagebreak() + +#num(exponent: "sci")[0] \ +#num(exponent: "sci")[0e4] \ +#num(exponent: "sci")[0.00] \ From e4ff8cb814e929380d2b4aee45f930bf53034663 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Fri, 27 Mar 2026 11:44:34 +0100 Subject: [PATCH 3/3] [add] tests --- tests/num/fixed/ref.typ | 3 ++- tests/num/fixed/test.typ | 1 + tests/num/trim-zeros/.gitignore | 5 +++++ tests/rounding/ref.typ | 14 ++++++++++++++ tests/rounding/test.typ | 23 +++++++++++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/num/trim-zeros/.gitignore create mode 100644 tests/rounding/ref.typ diff --git a/tests/num/fixed/ref.typ b/tests/num/fixed/ref.typ index ce99aa5..1ca14bd 100644 --- a/tests/num/fixed/ref.typ +++ b/tests/num/fixed/ref.typ @@ -7,4 +7,5 @@ $0.1234×10^1$ \ $1000×10^1$ \ $0.001×10^(-1)$ \ $(0.2 plus.minus 0.1)×10^1$ \ -$(0.2 plus.minus 0.1)×10^2$ +$(0.2 plus.minus 0.1)×10^2$ \ +$0.00$ \ diff --git a/tests/num/fixed/test.typ b/tests/num/fixed/test.typ index b7592b4..0d57a58 100644 --- a/tests/num/fixed/test.typ +++ b/tests/num/fixed/test.typ @@ -11,3 +11,4 @@ #num(exponent: (fixed: -1))[1e-4] \ #num(exponent: (fixed: 1))[2+-1] \ #num(exponent: (fixed: 2))[2+-1e1] \ +#num(exponent: (fixed: 2))[0.00] \ diff --git a/tests/num/trim-zeros/.gitignore b/tests/num/trim-zeros/.gitignore new file mode 100644 index 0000000..2e4c5d8 --- /dev/null +++ b/tests/num/trim-zeros/.gitignore @@ -0,0 +1,5 @@ +# generated by tytanic, do not edit + +/diff/ +/out/ +/ref/ diff --git a/tests/rounding/ref.typ b/tests/rounding/ref.typ new file mode 100644 index 0000000..17c3051 --- /dev/null +++ b/tests/rounding/ref.typ @@ -0,0 +1,14 @@ + +#set page(width: auto, height: auto, margin: .5em) + + +$0.000$ \ +$0.000$ \ +$0$ \ +$0.000$ \ + +#pagebreak() + +$423.4×10^2$ \ +$4.2×10^4$ \ +$42×10^3$ \ diff --git a/tests/rounding/test.typ b/tests/rounding/test.typ index e4e85af..f85f9d5 100644 --- a/tests/rounding/test.typ +++ b/tests/rounding/test.typ @@ -1,4 +1,5 @@ #import "/src/rounding.typ": * +#import "/src/zero.typ": num @@ -277,3 +278,25 @@ ), ("", "", none), ) + + + + + + +#set page(width: auto, height: auto, margin: .5em) + +// Rounding zero +#num(round: (precision: 3, mode: "places"))[0] \ +#num(round: (precision: 3, mode: "places"))[0.0] \ +#num(round: (precision: 3, mode: "figures"))[0] \ +#num(round: (precision: 3, mode: "places"), exponent: "sci")[0.0] + +#pagebreak() + +// Rounding affects displayed digits, (after the exponent is applied) +#num(round: (precision: 1), exponent: (fixed: 2))[42.34e3] \ +#num(round: (precision: 1), exponent: "sci")[42.34e3] \ +#num(round: (precision: 2, mode: "figures"), exponent: "eng")[42.34e3] \ + +