3

I plan to write a command which rounds a number with n digits. n is an optional argument and it is 2 by default. However, I keep receiving error message:

Missing number, treated as zero. Let's see if this prints: \round{3.1415926,2}

\documentclass{article}
\usepackage{tikz}

\newcommand{\round}[2][2]{\pgfmathparse{round({#1}*10^{#2})/(10^{#2})}\pgfmathresult}

\begin{document}
Let's see if this prints: \round{3.1415926,2}.
\end{document}

What have I done wrong? How to fix this code?

3

Apart from the issue with the additional comma, I think that you got the order of #1 and #2 wrong, and there are already means that do precisely what you want.

\documentclass{article}
\usepackage{tikz}

\newcommand{\round}[2][2]{
\pgfkeys{/pgf/number format/.cd,std,precision=#1}
\pgfmathprintnumber{#2}
}

\begin{document}
Let's see if this prints: \round{3.14159262}.
\end{document}
  • How to change the decimal separator using a comma? – pzorba75 May 24 '18 at 2:41
  • Thank you very much. I am passing the result of another expression to \round. It seems latex send the raw command to it rather than the result of the expression causing error. Is there any way to get around this problem? – ar2015 May 24 '18 at 2:44
  • 3
    @pzorba75 There is the option /pgf/number format/read comma as period=true|false, see section 92 of the pgfmanual. – user121799 May 24 '18 at 2:47
  • I follow it here. – ar2015 May 24 '18 at 3:29
  • @marmot, Thanks. I just leave it there in case anyone would like to follow the next question. – ar2015 May 24 '18 at 3:44

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.