I'd like to be able to output something like \frac{\pi}{2} for a input number like .5*pi automatically.
I've played a bit with some of the number printing macros available with tikz (closely linked to the fpu library, available at least in the cvs version).
The idea I had is to divide the number by pi, then use the number format/frac and then add a \pi to the result. But it does not work as expected as rounding errors occur. Here is the result for 2*pi, 1.5*pi and 0.5*pi.

Here is what I did so far.
\documentclass{standalone}
\usepackage{tikz,fp}
\usetikzlibrary{fpu}
\makeatletter
% Adapted from the file /generic/pgf/math/pgfmathfloat.code.tex
\pgfkeys{%
/pgf/number format/.cd,
pi/.code = \pgfmath@set@number@printer{pgfmathprintnumber@pi}}
\def\pgfmathprintnumber@pi#1{%
\pgfmathparse{#1/3.141592654}
\pgfmathfloatparsenumber{\pgfmathresult}%
\pgfmathfloatgetfrac{\pgfmathresult}%
\expandafter\pgfmathprintnumber@pi@formatresult\pgfmathresult}
% Nothing really new here, just a copy of
% \pgfmathprintnumber@frac@formatresult only altered to add
% \pi to the result
\def\pgfmathprintnumber@pi@formatresult#1#2#3{%
\begingroup
\pgfkeysgetvalue{/pgf/number format/frac TeX}\pgfmathresult
\toks0=\expandafter{\pgfmathresult}%
\def\pgfmathfloat@loc@TMPa{#1}%
\ifx\pgfmathfloat@loc@TMPa\pgfutil@empty
\ifpgfmathprintnumber@showpositive
\def\pgfmathfloat@loc@TMPa{+}%
\fi
\else
\ifx\pgfmathfloat@loc@TMPa-%
\else
\ifx\pgfmathfloat@loc@TMPa+%
\else
\ifpgfmathprintnumber@showpositive
\edef\pgfmathfloat@loc@TMPa{+\pgfmathfloat@loc@TMPa}%
\fi
\def\pgfmathfloat@loc@TMPb{%
\pgfkeysvalueof{/pgf/number format/frac whole format/.@cmd}}%
\expandafter\pgfmathfloat@loc@TMPb\pgfmathfloat@loc@TMPa\pgfeov
\let\pgfmathfloat@loc@TMPa=\pgfmathresult
\fi
\fi
\fi
\toks1=\expandafter{\pgfmathfloat@loc@TMPa}%
\edef\pgfmathresult{%
\the\toks1 \ifnum#2=0 \else\the\toks0 {#2}{#3}\fi \pi}%
\pgfmath@smuggleone\pgfmathresult
\endgroup
}
\begin{document}
\pgfkeys{/pgf/number format/pi}
\pgfmathprintnumber{2*pi}
\pgfmathprintnumber{1.5*pi}
\pgfmathprintnumber{0.25*pi}
\end{document}

fracformat: it is numerically instable. That means: it will magnify any (small) input errors considerably. See the result of Yiannidis for an illustration (that is inherent to the task, not to the impl.). Since the division by pi will already introduce a rounding error of order 1e-4 or 1e-5, you will always see distortions in the output. Perhaps it is possible to tune the algorithm if you provide additional knowledge (like the expected denominator). – Christian Feuersänger Feb 26 '12 at 16:05