-
I seem to be missing something obvious her, but how to you show numbers with thousands separators? My variable is e.g. an integer (or might be a float): How can I show this with thousands separators in my document?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I personally use my trusty formatting function. I don't know of any builtin functionality to do that though #let fmt-number(
number,
decimals: 2,
decimal-sep: ".",
thousands-sep: "'"
) = {
let integer = calc.trunc(number)
let decimal = calc.fract(number)
let res = str(integer).clusters()
.rev()
.chunks(3)
.map(c => c.join(""))
.join(thousands-sep)
.rev()
if decimals != 0 {
res += decimal-sep
decimal = decimal * calc.pow(10, decimals)
decimal = calc.round(decimal)
decimal = str(decimal) + ("0" * decimals)
res += decimal.slice(0, decimals)
}
return res
} |
Beta Was this translation helpful? Give feedback.
-
Hi @Erenedim , this also sounds like a use-case for zero, a package dedicated to number formatting. You can show numbers of all kinds via the central #import "@preview/zero:0.2.0": num, set-group
#set-group(separator: ",")
#let n = 123452112
#num(n) |
Beta Was this translation helpful? Give feedback.
Just want to add I found an error in your code.
If you format the number 3.08233 or 3.008233 it will be displayed as 3.82 (removing the zero in the decimal).
I have made an updated format function redoing the decimals part of the function and renaming decimals to digits to make it interchangeable with the build-in round function: