Why is there a decimal for 3x4=12.0?

First, you are not formatting the number when you say \pgfmathresult. Second, you have not specified any particular format for number printing. (Although the default format will do, in this case.)

Compare:

\documentclass{article}
\usepackage{pgf}
\begin{document}

\pgfmathparse{3*4}
\pgfmathresult

\pgfmathprintnumber\pgfmathresult

\pgfmathparse{int(3*4)}
\pgfmathresult

\pgfkeys{% just for example - doesn't actually matter here
  /pgf/number format/int detect,
}

\pgfmathparse{3*4}
\pgfmathprintnumber\pgfmathresult


\end{document}

comparison

PGF doesn't know you are typesetting Grade 2 maths rather than a doctoral dissertation in nuclear engineering!


The standard representation returned by PGF is with a decimal part, even if it is .0, due to how the internal work (it's TeX that adds .0 when requested to print the value of a \dimen register and PGF just strips the trailing pt), unless you declare the operation to return an integer.

An alternative is using expl3, whose floating point module adds a decimal part only if nonzero:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n % get a user level version
\ExplSyntaxOff

\begin{document}

\fpeval{3*4}

\end{document}

Note you don't even need the double step of \pgfmathresult. The accuracy of the floating point module is according to IEEE standards, contrary to what PGF provides.

Try the following with PGF and you'll know what road to take.

\documentclass{article}
\usepackage{expl3,siunitx}

\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n % get a user level version
\ExplSyntaxOff

\begin{document}

\fpeval{333*444}

\num[group-separator={\,}]{\fpeval{333*444}}

\num[group-separator={,}]{\fpeval{333*444}}

\end{document}

enter image description here

Tags:

Pgfmath