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/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/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/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/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] \ 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 ecc6265..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 @@ -258,3 +259,44 @@ ), ("1", "23", (("", "04"), ("", "30"))), ) +#assert.eq( + round( + "0", + "", + precision: 2, + mode: "places", + ), + ("", "00", none), +) + +#assert.eq( + round( + "0", + "", + precision: 2, + mode: "figures", + ), + ("", "", 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] \ + +