I’m on the latest development version of typst
❯ typst --version
typst 0.11.0 (fcdccc9c)
I have a table with numbers generated by another program, and I’d like to get them all to display as float values with 4 decimal places. For example
#table(columns:2,
[Thing], [number],
[a], [0.01],
[b], [0],
[c], [-0.005],
[d], [-0.0000001])
I would like to display the same as
#table(columns:2,
[Thing], [number],
[a], [0.0100],
[b], [0.0000],
[c], [-0.0050],
[d], [-0.0000])
At first, I thought I wanted the #calc.round()
function and was trying to make some #show
rules, but having a couple of problems. The first is that, even if I add the function manually, #calc.round()
doesn’t do exactly what I want
#table(columns:2,
[Thing], [number],
[a], [#calc.round(0.01, digits:4)],
[b], [#calc.round(0, digits:4)],
[c], [#calc.round(-0.005, digits:4)],
[d], [#calc.round(-0.0000001, digits:4)])
The second is that I can’t figure out how to get this via a show rule. The closest I got (I think) is something like
#show table.cell: it => {
if it.x == 1 and it.y != 0 {
calc.round(float(it))
}
else {
it
}
}
or the same without the call to float()
, and i get
error: expected float, boolean, integer, decimal, ratio, or string, found content
┌─ manuscript/main.typ:1076:25
│
1076 │ calc.round(float(it))
│ ^^
My lack of understanding clearly goes deeper, because I also attempted to use the Oxifmt package and
#import "@preview/oxifmt:0.2.1": strfmt
#table(columns:2,
[Thing], [number],
[a], [#strfmt("{0:.4}", 0.01)],
[b], [#strfmt("{0:.4}", 0)],
[c], [#strfmt("{0:.4}", -0.005)],
[d], [#strfmt("{0:.4}", -0.000001)])
Does the right thing (other than the integer 0, but I can get around that), but when I tried it in a show rule it didn’t work.