Regexes kind of work, as they always do, until they don’t. Your solution deletes all zeroes at the end of a number. #fmt(200) becomes 2.
I’d really like a solution that integrates with oxifmt because I want to use its fmt-thousands-separator and fmt-decimal-separator, and also because I feel it kind of is the way to go until Typst gets proper string formatting.
OK, so I filed a feature request to oxifmt but looking further into this, I think this is actually a Typst bug. Typst should convert a decimal to a string in the shortest possible way, not in the way the number was input. I think the following is wrong:
#let a = decimal("200")
#let b = decimal("200.0")
(a: #a, b: #b)
Naked comparison: a == b: #{a == b}
String comparison: str(a) == str(b): #{str(a) == str(b)}
This constructor preserves all given fractional digits, provided they are representable as per the limits specified below (otherwise, an error is raised).
I don’t know if I want to raise an issue for this, I can kind of see that keeping the digits might be handy in some circumstances. In the meantime, I learned that removing these trailing fractional zeroes is called normalisation. I also found a more robust way to do this that returns a new decimal: