How to left-pad an integer with zeroes to make it many digits?

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}}.

Perhaps the fastest (and fully expandable, if that matters) solution, without additional packages:

\zeropad{<template>}{<integer>}
% <template> string of zeros defining desired width of output
\documentclass{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\zeropad[2]{%
  \ifnum#2<0\relax%
    {\ensuremath-}\zeropadA{#1}{\the\numexpr#2*-1\relax}%
  \else%
    \zeropadA{#1}{#2}%
  \fi%
}
\def\zeropadA#1#2{%
  \ifnum1#2<1#1
    \zeropadA{#1}{0#2}%
  \else%
    #2%
  \fi%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{mynumber}
\makeatletter
\AtEndDocument{%
  \immediate\write\@auxout{%
    \string\setcounter{mynumber}{\themynumber}%
  }%
}
\makeatother
\AtBeginDocument{%
  \stepcounter{mynumber}
}

\begin{document}
the version number is \zeropad{0000}{\themynumber}

this is a negative number: \zeropad{0000}{-\themynumber}

expandibility check: \edef\foo{\zeropad{0000}{-\themynumber}}\foo
\end{document}

A fully expandable version

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\padinteger}{mm}
 {
  \int_compare:nNnTF { #2 } < { 0 }
   {
    - \kuttens_padinteger:nn { #1 } { -#2 }
   }
   {
    \kuttens_padinteger:nn { #1 } { #2 }
   }
 }

\cs_new:Nn \kuttens_padinteger:nn
 {
  \prg_replicate:nn { #1 - \tl_count:f { \int_to_arabic:n { #2 } } } { 0 }
  \int_to_arabic:n { #2 }
 }
\cs_generate_variant:Nn \tl_count:n { f }
\ExplSyntaxOff

% define the macro based on the number of digits you need, here four
\makeatletter
\newcommand{\padfour}[1]{\expandafter\@padfour\csname c@#1\endcsname}
\newcommand{\@padfour}[1]{\padinteger{4}{#1}}
\makeatother

\newcounter{mynumber}
\renewcommand{\themynumber}{\padfour{mynumber}}

\makeatletter 
\AtEndDocument{%
    \immediate\write\@auxout{%
        \string\setcounter{mynumber}{\themynumber}%
    }%
}
\makeatother
\AtBeginDocument{%
    \stepcounter{mynumber}
}


\begin{document}

\section{The version number}

Number: \themynumber

\section{Other tests}

\newcounter{test}

\padinteger{4}{\value{test}}

\setcounter{test}{23}

\padinteger{4}{\value{test}}

%\padinteger{4}{12345} % would raise an error

\padinteger{8}{12345}

\setcounter{test}{-123}

$\padinteger{4}{\value{test}}$

\end{document}

enter image description here