[Tex/LaTex] How to left-pad an integer with zeroes to make it many digits

formattingnumbering

A minimal working sample should be give first:

\documentclass{article}
\newcounter{mynumber}
\makeatletter 
\AtEndDocument{%
    \immediate\write\@auxout{%
        \string\setcounter{mynumber}{\number\value{mynumber}}%
    }%
}
\makeatother
\AtBeginDocument{%
    \stepcounter{mynumber}
}

\begin{document}
the version number is \themynumber.
\end{document}

Compiling it with XeLateX, we will get the version number such as 1. Then my QUESTION is

  • how to make 1 become 0001 and 23 become 0023 and so on (i.e., four-digit numbers)? Note that I want the format like 0001 but not 0 001 (i e., no unit, no space).

Best Answer

with package siunitx:

the version number is \num[minimum-integer-digits = 4]{\themynumber}.

and for Sans Serif

\textsf{\num[detect-family,minimum-integer-digits = 4]{\themynumber}}.