Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer

up vote 2 down vote accepted

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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