Suppose you want to print pi with 2 decimal digits. The following code does not work
$\sage{"%0.2f"%pi}$
What should be used instead?
Suppose you want to print pi with 2 decimal digits. The following code does not work
$\sage{"%0.2f"%pi}$
What should be used instead?
Try this:
\documentclass{article}
\usepackage{sagetex}
\begin{document}
\noindent Print $\pi$ as a number: $\sage{pi.n(digits=3)}$\\
Print $\pi$ as a string: \sagestr{str(pi.n(digits=3))}
\end{document}
Here is the output in Sagemath Cloud:

As I mention here there are issues with digits and using sage that I don't understand. Using sagestr has been better for me.
DJP's answer is good; I will add that "pi" in Sage is not a float; it is a (Sage/Python) object that represents the real number pi.
You did not describe the error, but I believe what is going wrong is that LaTeX is interpreting the % as a comment character. There is a way to use your string formatting method, but the idiomatic way to get
a decimal approximation in Sage is to use the .n() method. Here are some examples:
$\sage{pi.n()}$
$\sage{pi.n(10)}$ which returns an approximation with 10 bits of accuracy
$\sage{pi.n(digits=10)}$ which returns an approximation accurate to 10 significant figures
$\sage{pi.n(digits=10000)}$ if you want to fill several screens full of digits
If you use Sage, you really should get used to using .n() on various things. Open a worksheet on SageMathCloud and try it on things like pi, sqrt(5), e, log(123), and so on. Also feel free to ask questions on the psage-support Google Group](https://groups.google.com/forum/#!forum/sage-support).
Can this help?
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro \pi {pi}
\node at (0,0) {\pgfmathprintnumber[fixed,precision=2]{\pi}};
\node at (0,-1) {\pgfmathprintnumber[fixed,precision=4]{\pi}};
\node at (0,-2) {\pgfmathprintnumber[fixed,precision=6]{\pi}};
\end{tikzpicture}
\end{document}

sagetex rather than asking about an entirely different method. You are the second person to offer an answer of this kind so I think maybe the question should be clarified.
– cfr
Jul 13 '15 at 02:58
$\sage{"\%0.2f"\%pi}$work maybe by escaping the percent chars? If this is Python then maybe you can also use the new string formatting$\sage{'{0:.2f}'.format(pi)}$– percusse Jul 11 '15 at 20:52sagetexmethods e.g. using Lua or PGF. I think if you could make it more obvious that you want asagetexsolution, that would be helpful. The 'What should be used instead?' is being understood as 'I'm open to non-sagetexsolutions'. Alternatively, if you are open to such solutions, of course make that clear instead. – cfr Jul 13 '15 at 03:00