diff --git a/README.md b/README.md index a00db10..d66e9ec 100644 --- a/README.md +++ b/README.md @@ -210,8 +210,9 @@ 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. 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. diff --git a/src/rounding.typ b/src/rounding.typ index d8199b2..8dee27e 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 @@ -112,6 +113,9 @@ int = number.slice(0, new-int-digits) frac = number.slice(new-int-digits) + } else if type(pad) == std.int { + 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()) } @@ -144,8 +148,10 @@ 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 (`mode: "places"`) or significant figures (`mode: "figures"`) + /// to display. The parameter `pad` has no effect in `mode: "uncertainty"`. + /// -> bool | int pad: true, /// Uncertainty pm: none, @@ -192,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( @@ -206,6 +212,7 @@ place + u.first().len(), dir: direction, pad: true, + precision: precision )) } } @@ -219,6 +226,7 @@ pad: pad, sign: sign, ties: ties, + precision: precision ), pm, ) 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))