How to convert a one digit number to a two digit number

You can define a macro as follows:

\newcommand\twodigits[1]{%
   \ifnum#1<10 0#1\else #1\fi
}

\twodigits{12}  % 12
\twodigits{4}   % 04
\twodigits{123} % 123

This macro is fully expandable.

If you also want to cut trailing zeros you can use:

\newcommand\twodigits[1]{%
   \ifnum#1<10 0\number#1 \else #1\fi
}

\twodigits{004} % 04

If you want to change a tabular cell from 12 4 to 12 04 without adding explicit macros you can use the collcell package to collect the cell content and feed it to a macro which splits the numbers by the space:

% preamble:
\newcommand\formatdate[1]{\formatdatei#1\relax}
\def\formatdatei#1 #2\relax{%
   \twodigits{#1} \twodigits{#2}%
}

\usepackage{collcell}

% later
\begin{tabular}{l>{\collectcell\formatdate}l<{\endcollectcell}}
   Bus date  & 12 4 \\
\end{tabular}

If you post a real usage example I can help me with more specific macros.


You can do that using the siunitx package

\documentclass[a4paper,twoside,10pt]{article}

\usepackage{siunitx}

\begin{document}
\num[minimum-integer-digits = 4]{123}
\num[minimum-integer-digits = 4]{4}
\end{document}

prints

0123 0004

or alternatively you can declare the option as default,

\documentclass[a4paper,twoside,10pt]{article}
\usepackage{siunitx}

\begin{document}
\sisetup{minimum-integer-digits = 4}
\num{12}
\num{34}
\end{document}

You can also use the xstring package to add the leading zero for single digits:

enter image description here

Code:

\documentclass{article}
\usepackage{xstring}
\newcommand*{\TwoDigit}[1]{%
    \IfStrEqCase{#1}{%
        {1}{0}%
        {2}{0}%
        {3}{0}%
        {4}{0}%
        {5}{0}%
        {6}{0}%
        {7}{0}%
        {8}{0}%
        {9}{0}%
    }#1%
}%
\begin{document}
123 $\to$ \TwoDigit{123}

12 $\to$ \TwoDigit{12}

2 $\to$  \TwoDigit{2}
\end{document}