From 98dd37012b46463c04e6fc0b4235e8fec2016ebb Mon Sep 17 00:00:00 2001 From: initramfs Date: Thu, 18 Dec 2025 08:14:15 +0000 Subject: [PATCH 1/7] [add] variable padding in rounding mode --- README.md | 4 ++-- src/rounding.typ | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a00db10..cc78c37 100644 --- a/README.md +++ b/README.md @@ -210,8 +210,8 @@ Rounding can be configured with the `set-round()` function. then the number is rounded to the same number of decimal places as the uncertainty. - `precision: int | none = 2` : The precision to round to. Also see parameter `mode`. When set to `none`, no rounding is applied. -- `pad: bool = true` : Whether to pad the number with zeros if the - number has fewer digits than the rounding precision. +- `pad: bool | int = true` : Whether to pad the number with zeros if the + number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display. - `direction: str = "nearest"` : Sets the rounding direction. - `"nearest"`: Rounding takes place in the usual fashion, rounding to the nearer number, e.g., 2.3 → 2 and 2.6 → 3. diff --git a/src/rounding.typ b/src/rounding.typ index d8199b2..4d60cb6 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -112,6 +112,8 @@ int = number.slice(0, new-int-digits) frac = number.slice(new-int-digits) + } else if type(pad) == std.int { + frac += "0" * calc.clamp(pad - frac.len(), 0, total-digits - number.len()) } else if pad { frac += "0" * (total-digits - number.len()) } @@ -144,8 +146,9 @@ direction: "nearest", ties: "away-from-zero", /// Determines whether the number should be padded with zeros if the number has less - /// digits than the rounding precision. - /// -> bool + /// digits than the rounding precision. If an integer is given, determines the minimum + /// number of decimal digits to display. + /// -> bool | int pad: true, /// Uncertainty pm: none, From 688f6c339edeb1ce5ad3b19c673ae0ede6821e2f Mon Sep 17 00:00:00 2001 From: initramfs Date: Thu, 18 Dec 2025 16:02:48 +0000 Subject: [PATCH 2/7] [fix] make variable pad semantics follow mode in rounding --- README.md | 3 ++- src/rounding.typ | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc78c37..c1eb64d 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,8 @@ Rounding can be configured with the `set-round()` function. uncertainty. - `precision: int | none = 2` : The precision to round to. Also see parameter `mode`. When set to `none`, no rounding is applied. - `pad: bool | int = true` : Whether to pad the number with zeros if the - number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display. + number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display + in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. - `direction: str = "nearest"` : Sets the rounding direction. - `"nearest"`: Rounding takes place in the usual fashion, rounding to the nearer number, e.g., 2.3 → 2 and 2.6 → 3. diff --git a/src/rounding.typ b/src/rounding.typ index 4d60cb6..d3e18d6 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -94,6 +94,7 @@ dir: "nearest", ties: "away-from-zero", pad: true, + precision: 2 ) = { total-digits = calc.max(0, total-digits) let number = int + frac @@ -113,7 +114,8 @@ int = number.slice(0, new-int-digits) frac = number.slice(new-int-digits) } else if type(pad) == std.int { - frac += "0" * calc.clamp(pad - frac.len(), 0, total-digits - number.len()) + let maxPad = total-digits - number.len() + frac += "0" * calc.clamp(pad - precision + maxPad, 0, maxPad) } else if pad { frac += "0" * (total-digits - number.len()) } @@ -147,7 +149,8 @@ ties: "away-from-zero", /// Determines whether the number should be padded with zeros if the number has less /// digits than the rounding precision. If an integer is given, determines the minimum - /// number of decimal digits to display. + /// number of decimal digits (mode: "places") or significant figures (mode: "figures") + /// to display. /// -> bool | int pad: true, /// Uncertainty @@ -195,7 +198,7 @@ let is-symmetric = type(pm.first()) != array if is-symmetric { round-digit-pm = count-leading-zeros(pm.join()) + precision - pm = round-or-pad(..pm, round-digit-pm, dir: direction, pad: true) + pm = round-or-pad(..pm, round-digit-pm, dir: direction, pad: true, precision: precision) round-digit = round-digit-pm + int.len() - pm.first().len() } else { let place = calc.max( @@ -209,6 +212,7 @@ place + u.first().len(), dir: direction, pad: true, + precision: precision )) } } @@ -222,6 +226,7 @@ pad: pad, sign: sign, ties: ties, + precision: precision ), pm, ) From 6c0d31dab83d86632582168ea3590eb378c0572f Mon Sep 17 00:00:00 2001 From: initramfs Date: Thu, 18 Dec 2025 17:38:15 +0000 Subject: [PATCH 3/7] [tests] add tests for variable pad in rounding --- tests/rounding/test.typ | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/rounding/test.typ b/tests/rounding/test.typ index 3b26581..3330a68 100644 --- a/tests/rounding/test.typ +++ b/tests/rounding/test.typ @@ -136,8 +136,13 @@ #assert.eq(round-places("1", "1", precision: 3), ("1", "100", none)) #assert.eq(round-places("1", "1", precision: 5), ("1", "10000", none)) #assert.eq(round-places("1", "1", precision: 5), ("1", "10000", none)) -#assert.eq(round-places("1", "1", precision: 5, pad: false), ("1", "1", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: false), ("1", "1", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: true), ("1", "10000", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: -1), ("1", "1", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: 0), ("1", "1", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: 3), ("1", "100", none)) +#assert.eq(round-places("1", "1", precision: 5, pad: 6), ("1", "10000", none)) #assert.eq(round-figures("1", "234", precision: 4), ("1", "234", none)) #assert.eq(round-figures("1", "234", precision: 3), ("1", "23", none)) @@ -147,11 +152,23 @@ #assert.eq(round-figures("1", "234", precision: -1), ("0", "", none)) #assert.eq(round-figures("1", "2", precision: 4), ("1", "200", none)) + #assert.eq(round-figures("1", "2", precision: 4, pad: false), ("1", "2", none)) +#assert.eq(round-figures("1", "2", precision: 4, pad: true), ("1", "200", none)) +#assert.eq(round-figures("1", "2", precision: 4, pad: -1), ("1", "2", none)) +#assert.eq(round-figures("1", "2", precision: 4, pad: 0), ("1", "2", none)) +#assert.eq(round-figures("1", "2", precision: 4, pad: 3), ("1", "20", none)) +#assert.eq(round-figures("1", "2", precision: 4, pad: 6), ("1", "200", none)) #assert.eq(round-figures("0", "00126", precision: 2), ("0", "0013", none)) #assert.eq(round-figures("0", "000126", precision: 3), ("0", "000126", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: false), ("0", "0016", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: true), ("0", "001600", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: -1), ("0", "0016", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: 0), ("0", "0016", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: 3), ("0", "00160", none)) +#assert.eq(round-figures("0", "0016", precision: 4, pad: 6), ("0", "001600", none)) #assert.eq(round-places("99", "92", precision: 2), ("99", "92", none)) From e2f06a2bbbe908a506860074518d2abb4f7f7d35 Mon Sep 17 00:00:00 2001 From: initramfs Date: Thu, 18 Dec 2025 17:41:31 +0000 Subject: [PATCH 4/7] [docs] clarify effects of `pad` parameter (rounding) Explicitly specify that the `pad` parameter in rounding has no effect when `mode` is set to "uncertainty". --- README.md | 2 +- src/rounding.typ | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c1eb64d..66ff49a 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ Rounding can be configured with the `set-round()` function. - `precision: int | none = 2` : The precision to round to. Also see parameter `mode`. When set to `none`, no rounding is applied. - `pad: bool | int = true` : Whether to pad the number with zeros if the number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display - in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. + in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. `pad` has no effect in `mode: "uncertainty"`. - `direction: str = "nearest"` : Sets the rounding direction. - `"nearest"`: Rounding takes place in the usual fashion, rounding to the nearer number, e.g., 2.3 → 2 and 2.6 → 3. diff --git a/src/rounding.typ b/src/rounding.typ index d3e18d6..a6d2c51 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -149,8 +149,8 @@ ties: "away-from-zero", /// Determines whether the number should be padded with zeros if the number has less /// digits than the rounding precision. If an integer is given, determines the minimum - /// number of decimal digits (mode: "places") or significant figures (mode: "figures") - /// to display. + /// number of decimal digits (`mode: "places"`) or significant figures (`mode: "figures"`) + /// to display. `pad` has no effect in `mode: "uncertainty"`. /// -> bool | int pad: true, /// Uncertainty From 00e2ad7f327f923589f6d5ef6755be5e3b1786e9 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 18 Dec 2025 19:37:41 +0100 Subject: [PATCH 5/7] [docs] start phrase with a proper word. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 66ff49a..596a5c1 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ Rounding can be configured with the `set-round()` function. - `precision: int | none = 2` : The precision to round to. Also see parameter `mode`. When set to `none`, no rounding is applied. - `pad: bool | int = true` : Whether to pad the number with zeros if the number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display - in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. `pad` has no effect in `mode: "uncertainty"`. + in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. The parameter `pad` has no effect in `mode: "uncertainty"`. - `direction: str = "nearest"` : Sets the rounding direction. - `"nearest"`: Rounding takes place in the usual fashion, rounding to the nearer number, e.g., 2.3 → 2 and 2.6 → 3. From 691131d26aba8f5b10e89ed115b7cac742ee99aa Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 18 Dec 2025 22:46:23 +0100 Subject: [PATCH 6/7] [refactor] case to kebab case --- src/rounding.typ | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rounding.typ b/src/rounding.typ index a6d2c51..8dee27e 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -114,8 +114,8 @@ int = number.slice(0, new-int-digits) frac = number.slice(new-int-digits) } else if type(pad) == std.int { - let maxPad = total-digits - number.len() - frac += "0" * calc.clamp(pad - precision + maxPad, 0, maxPad) + let max-pad = total-digits - number.len() + frac += "0" * calc.clamp(pad - precision + max-pad, 0, max-pad) } else if pad { frac += "0" * (total-digits - number.len()) } @@ -150,7 +150,7 @@ /// Determines whether the number should be padded with zeros if the number has less /// digits than the rounding precision. If an integer is given, determines the minimum /// number of decimal digits (`mode: "places"`) or significant figures (`mode: "figures"`) - /// to display. `pad` has no effect in `mode: "uncertainty"`. + /// to display. The parameter `pad` has no effect in `mode: "uncertainty"`. /// -> bool | int pad: true, /// Uncertainty From 56034a49038005efc2a74ffc269f98bf3bcfa343 Mon Sep 17 00:00:00 2001 From: Mc-Zen <52877387+Mc-Zen@users.noreply.github.com> Date: Thu, 18 Dec 2025 22:49:28 +0100 Subject: [PATCH 7/7] [docs] improve wording in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 596a5c1..d66e9ec 100644 --- a/README.md +++ b/README.md @@ -211,8 +211,8 @@ Rounding can be configured with the `set-round()` function. uncertainty. - `precision: int | none = 2` : The precision to round to. Also see parameter `mode`. When set to `none`, no rounding is applied. - `pad: bool | int = true` : Whether to pad the number with zeros if the - number has fewer digits than the rounding precision. If set to an integer, the minimum number of decimal digits to display - in `mode: "places"` or the minimum number of significant figures in `mode: "figures"`. The parameter `pad` has no effect in `mode: "uncertainty"`. + number has fewer digits than the rounding precision. When an integer is passed, it defines the minimum number of decimal digits to display + for `mode: "places"` and the minimum number of significant figures for `mode: "figures"`. The parameter `pad` has no effect for `mode: "uncertainty"`. - `direction: str = "nearest"` : Sets the rounding direction. - `"nearest"`: Rounding takes place in the usual fashion, rounding to the nearer number, e.g., 2.3 → 2 and 2.6 → 3.