I have a table that represents a timetable for bus, so each cell contains time of departure as hour followed by minute, both as two digits, but sometimes I don't have two digits,just one so I have to switch eg. from 12 4 -> 12 04. How is it possible?


4 Answers 4


You can define a macro as follows:

   \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:

   \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:
\def\formatdatei#1 #2\relax{%
   \twodigits{#1} \twodigits{#2}%


% later
   Bus date  & 12 4 \\

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

  • Thank you for response. Can you please take a look here? I've added the question before with a case usage, but someone suggested to post a different question regarding this problem. May 17, 2012 at 10:46
  • @IonMorozan: In such cases always add a link to the other question in the new question. May 17, 2012 at 10:48
  • Yes you are right! It's is my third post on a forum, so I have to learn. Thank you for the feedback and by the way your solution works great for me. Thank you again! May 17, 2012 at 10:51
  • @MarcoDaniel: Thanks for pointing this out. I wrote the answer under my Windows installation were I don't have LaTeX installed, so I couldn't test it right away. I added a version which handles this kind of input without an assignment. May 17, 2012 at 12:50
  • 1
    @MartinScharrer: I found the following definition in latex.ltx ;-) \def\two@digits#1{\ifnum#1<10 0\fi\number#1} May 17, 2012 at 13:18

You can do that using the siunitx package



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


0123 0004

or alternatively you can declare the option as default,


\sisetup{minimum-integer-digits = 4}
  • 3
    Can you set minimum-integer-digits separately, as a default? May 17, 2012 at 11:56
  • @percusse thanks for the edit, wasn't in front of a computer all day long.. May 17, 2012 at 22:35

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

enter image description here


123 $\to$ \TwoDigit{123}

12 $\to$ \TwoDigit{12}

2 $\to$  \TwoDigit{2}

You can use LaTeX's internal \two@digit for formatting:

enter image description here


\twodigit{12} \par % 12
\twodigit{4} \par  % 04
\twodigit{123}     % 123


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.