#let s="123"
#let i = ?? // what goes here
#let w = "123"
#let x = "123.6"
#let y = int(w) // this is for integers, it will error if you give a float
#let z = float(x) // this also works for integers
#x \ // this is just the string
#(y + z) // you can do operations on the numbers
#y // or use them as is
as a tip, you can find functions like this in the documentation by searching for the data type (e.g. integer, string, float…).
thanks, but I have the following bit of code:
#import "@preview/oxifmt:0.2.1": strfmt
#show table.cell.where(y:0):set text(weight:"extrabold")
#show table.cell: it => {
if it.y>0 {
let ss = it.body.text
if ss.contains("[a-zA-Z]") == false{
let ii = int(ss)
strfmt("{}", ii, fmt-thousands-separator: ",")
}else{
it
}
} else{
it
}
}
#let results = csv.decode("
Header1,Header2,Header3
A,B,231
C,D,2312
E,F,4546
G,H,6786
")
#let (header, ..data) = results
#table(
inset: (x:1em,y:1em),
columns: header.map(x=>1fr),
stroke: none,
fill: (x, y) => if calc.odd(y) { rgb("#eee") },
table.hline(),
table.header(..header),
table.hline(),
..data.flatten(),
table.hline()
)
and I get error like
error: invalid integer: A
┌─ test.typ:7:16
│
7 │ let ii = int(ss)
│ ^^
Which imply the string checking is not working, is there any way to tryParse() a number?
That is because your if ss.contains("[a-zA-Z]")
check is wrong, you’re not using regex here but literally checking for the string "[a-zA-Z]"
. If you wrap it with regex("[a-zA-Z]")
, it works.
PS: You can use ```typ``` to mark your code blocks to use Typst syntax highlighting, it will look a lot better!
Thanks a lot!
The documentation was not clear on this.
Refactored the code to be more robust and not have side effects:
#import "@preview/oxifmt:0.2.1": strfmt
#show table.cell.where(y:0):set text(weight:"extrabold")
#let results = csv.decode("
Header1,Header2,Header3
A,B,231
C,D,2312
E,F,4546
G,H,6786
")
#let (header, ..data) = results
#table(
inset: (x:1em,y:1em),
columns: header.map(x=>1fr),
stroke: none,
fill: (x, y) => if calc.odd(y) { rgb("#eee") },
table.hline(),
table.header(..header),
table.hline(),
..data.flatten().map(x=>{
if x.contains(regex("\D")) == false{
let ii = float(x)
strfmt("{}", ii, fmt-thousands-separator: ",")
}else{
x
}
}),
table.hline()
)