Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions src/rounding.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -206,6 +212,7 @@
place + u.first().len(),
dir: direction,
pad: true,
precision: precision
))
}
}
Expand All @@ -219,6 +226,7 @@
pad: pad,
sign: sign,
ties: ties,
precision: precision
),
pm,
)
Expand Down
19 changes: 18 additions & 1 deletion tests/rounding/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down