# fixed leading 0's with pgfmath

I want to calculate a time being printed using pgfmath. I have some code similiar to this:

\pgfmathparse{int(mod(\x,60))}\pgfmathresult O'clock


but if the result is between 0 and 9 my time being displayed is something like 11:2 instead of 11:02. Can I force fixed zeros at the front? I only found something to get them at the end.

-
Welcome to TeX.sx! You can use \ifdim to test the result. \ifdim\pgfmathresult<10 0\pgfmathresult\else\pgfmathresult\fi~O'clock – Marco Daniel Oct 10 '12 at 14:19
A slightly convoluted workaround would be to divide the number by 100 and print the result without the leading zero and without the decimal separator: 11:\pgfmathparse{int(mod(\x,60))/100}\pgfmathprintnumber[skip 0.=true, dec sep={}, fixed]{\pgfmathresult} – Jake Oct 10 '12 at 14:23
@Jake: Awesome idea ;-) – Marco Daniel Oct 10 '12 at 14:25
@Jake: thanks, this worked like charme :) – user19760 Oct 10 '12 at 14:27

pgfmath can use strings in its calculations, so you could test whether your result is less than 10 and add a 0 in that case:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\foreach \x in {2,4,...,14}{
11:\pgfmathparse{mod(\x,60)<10?"0":{},int(mod(\x,60))}\pgfmathresult\hspace{1em}
}
\end{document}


Another workaround would be to divide the number by 100 and print the output without the leading zero and without the decimal marker:

11:\pgfmathparse{int(mod(\x,60))/100}\pgfmathprintnumber[skip 0.=true, dec sep={}, fixed]{\pgfmathresult}

-